By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,269 Members | 1,561 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,269 IT Pros & Developers. It's quick & easy.

Compare dates - iso format

P: 14
Hi everybody

The date values entered in my text boxes (startDate and endDate) in my form are in ISO format YYYY-MM-DD.

Could you please help me and explain how I can compare the date values to make sure the start date is smaller than the end date?

I have something like the following, but it is not working (I guess due to ISO format)

Expand|Select|Wrap|Line Numbers
  1. if ( new Date(document.SearchFrm.startDate.value) > new Date(document.SearchFrm.endDate.value)){
  2. alert ("Start Date is greater");
  3. }

Any tips is extremely appreciated,
Maral
Nov 17 '07 #1
Share this Question
Share on Google+
5 Replies


acoder
Expert Mod 15k+
P: 16,027
Take the values and use split("-") to split the string into an array, then use setFullYear to set the date - see link.
Nov 17 '07 #2

P: 14
Take the values and use split("-") to split the string into an array, then use setFullYear to set the date - see link.

Thank you for your advice.
Maral
Nov 18 '07 #3

acoder
Expert Mod 15k+
P: 16,027
You're welcome. Let us know if you hit any problems. Good luck!
Nov 18 '07 #4

P: 14
Hi

Appreciate your help and advice with the problem I have faced. My code is as follows:

Expand|Select|Wrap|Line Numbers
  1. function Compare() {
  2.  
  3.     var StartDateSt=document.SearchFrm.startDate.value;
  4.     var EndDateSt=document.SearchFrm.endDate.value;
  5.  
  6.     StartDateSt = StartDateSt.split("-");
  7.     EndDateSt = EndDateSt.split("-");
  8.  
  9.     var StartDate=new Date();
  10.     StartDate.setFullYear(StartDateSt);
  11.  
  12.     var EndDate=new Date();
  13.     EndDate.setFullYear(EndDateSt);
  14.  
  15.    alert(EndDate);
  16.    alert(StartDate);
  17.  
  18. }

As the code is run, the two alert boxes return NaN.


Trying to solve the problem in a different way, this time I let the user enter the date the usual way dd/mm/yyyy and use something like :

Expand|Select|Wrap|Line Numbers
  1.     var StartDate = new Date(StartDateSt);
  2.     var StartDateYear = StartDate.getYear();
  3.     var StartDateMonth = StartDate.getMonth();
  4.     var StartDateDay = StartDate.getDate();  

and then connect the different parts through "-". The problem I face here is that, the entered date in the textbox of my Web page is only processed correctly if it is entered as mm/dd/yyyy; Is this the default setting of dates in Javascript?

How can I change the date format so the user can enter it as dd/mm/yyyy?


Thank you in advance
Maral
Nov 24 '07 #5

acoder
Expert Mod 15k+
P: 16,027
Expand|Select|Wrap|Line Numbers
  1. StartDate.setFullYear(StartDateSt);
should be
Expand|Select|Wrap|Line Numbers
  1. StartDate.setFullYear(StartDateSt[0],StartDateSt[1],StartDateSt[2]);
Nov 26 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.