Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to compare two dates (say Date of Birth with Current date) in Javascript in dd/ mm/yyyy format and mm/dd/yyyy format?
Posted
Updated 16-Mar-14 17:29pm
v2
Comments
What have you tried and where is the problem?

 
Share this answer
 
DateTime Firstdate= new DateTime(2000, 1, 19, 0, 0, 0, 0);
DateTime Second= new DateTime();

Days d = Days.daysBetween(Firstdate, Second);
int days = d.getDays();

System.out.println(" Difference between " + Second);
System.out.println(" and " + Firstdate+ " is " + days + " days.");
 
Share this answer
 
You should use a jQuery datepicker for the following reasons:
1. No need to write code to validate users' inputs for date (that is the biggest hassle of all);
2. Easily customizable date format for display.
3. No server side round trips.
Adapt the following code to your need:
XML
<!doctype html>
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$(function() {

   var format = "dd/mm/yy";
   var $this = $("#datepicker");
   var newDate = $this.datepicker({
    dateFormat: format,
    onSelect: function()
    {
       var newDate= $(this).datepicker('getDate');
       var curDateTime = new Date();
       var curDate = new Date(curDateTime.getFullYear(), curDateTime.getMonth(), curDateTime.getDate());
       if (curDate  > newDate ) {
            alert('The new date is earlier than today');
         } else if (curDate  < newDate ){
           alert('The new date is later than today');
         } else {
           alert('The new date is today');
         }
    }
 });
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker"></p>
</body>
</html>

Learn more jqueryui.datepicker[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900