Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a date (ie toDate) in "dd/MM/yyyy" format. Now i want to compare it with today's date in javascript
like
JavaScript
if(toDate>=todaysDate)
{
}


can any one please help me to solve this ?

thanks in advance
(Keerthi Kumar)
Posted

If you date is in string, then you have to split them out into the various date parts by using split[^]method. To avoid this hassle, you should consider using a datepicker like https://jqueryui.com/datepicker/[^]
Next, use the appropriate Date[^] methods to create the date objects.
Try this example and adapt:
<script>
var somedayString = "16/4/2015";
var dateParts= somedayString.split("/");
var day = dateParts[0];
var month = dateParts[1] - 1;
var year = dateParts[2];
// set a date
someday = new Date(year, month, day);
alert("someday is " + someday);

// set a date
var today = new Date();
today.setHours(0,0,0,0);
alert("today is " + today);

if (today < someday) {
    text = "Today is before someday.";
} else if (today > someday) {
    text = "Today is after someday.";
} else {
    text = "Someday is today.";
}
alert(text);
</script>

Note that the month numbers start from 0 which represents January.
 
Share this answer
 
v2
JavaScript
// try here
 var currentDate = new Date()
 var day = currentDate.getDate()
 var month = currentDate.getMonth() + 1
 var year = currentDate.getFullYear()
 document.write("" + day + "/" + month + "/" + year + "")
 var todaysDate = day + "/" + month + "/" + year
 
Share this answer
 

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