Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I read a date from Calendar control in a variable. The date may be in format dd-mm-yyyy or dd/mm/yyyy or mm-dd-yyyy or mm/dd/yyyy.
Then in another variable I get today's date.
How to convert the date value in string variable to mm/dd/yyyy format so that I can compare the two dates?
I want to check which date is bigger.

Here is my code
JavaScript
var date = objParam[0].value;
var todaysDate = new Date();
todaysDate = todaysDate.getMonth() + 1 + '/' + todaysDate.getDate() + '/' + todaysDate.getFullYear();

if (date > todaysDate) {
    alert("Please select the valid date");
}


Any help appreciated.

Thanks,
Lok
Posted

Have a look at : http://momentjs.com/docs/[^]

This one will be useful for you : http://momentjs.com/docs/#/displaying/difference/[^]

It provides a wide range of functions to handle the Date. Remember, date doesn't have format. It is Date's string representation.

Regards..
 
Share this answer
 
v3
Whats the sens of the following line?:

JavaScript
todaysDate = todaysDate.getMonth() + 1 + '/' + todaysDate.getDate() + '/' + todaysDate.getFullYear();



I did in my last project like this (I had to control if a token is expired):

JavaScript
var today = new Date();
if (compareTime > today.getTime()) {
...
}
 
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