0

Sometime back i had asked question in SO as to how to validate startdate < enddate.

The JS that i had written is as below;

function DateValidation(startDate, endDate) {
                if (startDate != ' ' && endDate != ' ') 
                {
                    var stdate = Date.parse(startDate);
                    var enddate = Date.parse(endDate);
                    if (stdate > enddate) 
                    {
                        alert('Start date cannot be greater than end date!');
                        return false;
                    }
                    else 
                {
                    return true;
                }
               }
            }

The function gets called on the click of the submit button. It is register as;

OnClientClick="javascript: return DateValidation(window.document.getElementById('txtStartDate').value,window.document.getElementById('txtEndDate').value);"

Now assume that start date is October 4 2011 (04/10/2011) and end date is December 2 2011 (02/12/2011). In this case the alert should NOT get fired but it still gets fired up (because 04 > 02).

What mistake am i making here?

Community
  • 1
  • 1
xorpower
  • 17,975
  • 51
  • 129
  • 180

3 Answers3

0
<script type="text/javascript">
var d = Date.parse("Jul 8, 2005");
document.write(d);
</script>

The output of the code above will be: 1120773600000

So your reasoning cannot be right. Try to alert the stdate and enddate after parsing. Something is happening there.

dinesh707
  • 12,106
  • 22
  • 84
  • 134
0

The mistake that you are making is that Date.parse interprets xx/xx/xxxx formatted strings as U.S. style dates with the month first. 04/10/2011 is taken as April 10, not October 4.

Here is a transcript:

> var startDate = '04/10/2011';var stdate = Date.parse(startDate)
> startDate
04/10/2011
> stdate
1302418800000
> var endDate = '02/12/2011';var enddate = Date.parse(endDate);
> endDate
02/12/2011
> enddate
1297497600000
> stdate > enddate
true

IMHO you should avoid ambiguous date formats like this. If date strings are to be used, force users to enter ISO8601 (yyyy/MM/dd) formatted text.

There is decent support for ISO8601 in ECMAScript 5 browsers. Also see the Date.js library. Or also this SO question for more info.

ADDENDUM

The built-in Date.parse function is documented here. You can see it does not support DD/MM/YYYY. While an evil programmer could accept a string in the form DD/MM/YYYY and use substring and concat (or regexes) to rewrite in an acceptable format for Date.parse, I'll assume you are not an evil programmer.

The proper solution is to use a date parser that accepts format strings. For JavaScript, one such library is Date.js. It will allow you to write the following:

Date.parseExact("20/04/2011", "dd/MM/yyyy");

and get the date object corresponding to April 20, 2011.

Community
  • 1
  • 1
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • What would you like to do? By "correct" do you mean you want people to enter DD/MM/YYYY? Or can you accept other formats? I can add to my answer; let me know which way you want to go. – Ray Toal Oct 04 '11 at 08:45
  • I want date format to be keep as it is 04/10 should be 4th october and not 10th April – xorpower Oct 04 '11 at 08:54
  • there is nothing like Date.parseExact in Javascript – xorpower Oct 04 '11 at 09:12
  • @Xorpower, `Date.parseExact` comes from the Date.js library cited in the answer. If you do not want to use a third-party library for some reason you are free to muck around with the string and build the date yourself. This is doable with regexes as I mentioned but IMHO this is a hack. If you are doing any serious date processing you should use a library, and Date.js is fairly well-known. – Ray Toal Oct 04 '11 at 13:32
0

Your startDate & endDate parameters should be in mm/dd/yyyy format. But you provided them as dd/mm/yyyy format, so the unexpected thing occured.

I've faced this kinda probs B4. If your website's clients are all about to type in date time in format "dd/mm/yyyy", so you need a "correctDate()" function to replace date part with month part in the input-date-string.

The function may look like this :

function correctDate(D){
       var D=D.split('/');
       return D[1]+'/'+D[0]+'/'+D[2]
}
//input :  2/10/2011 , output :  10/2/2011

And in the DateValidation() function, you can use it like this:

....
      var stdate = Date.parse( correctDate(startDate) );
      var enddate = Date.parse( correctDate(endDate) );
      if (stdate > enddate){
....
vantrung -cuncon
  • 10,207
  • 5
  • 47
  • 62
  • But that would change the date as a whole. In your e.g,. 2/10 is Oct 2 which would change to Feb 10?? – xorpower Oct 04 '11 at 09:20
  • plus looking to my question, where would i call the correctDate function? – xorpower Oct 04 '11 at 09:21
  • As I said in the answer, "If your website's clients are all about to type in date time in format "dd/mm/yyyy", so, you can use my method. How can you check the value : 2/10/2011 if you don't know the user means "2" is the "2nd" or "Feb" ? Is this value is valid compared to "5/5/2011" ??? – vantrung -cuncon Oct 04 '11 at 09:28
  • You can use the "correctDate" function inside your DateValidation function. As in my answer: "...var stdate = Date.parse( correctDate(startDate) );...." – vantrung -cuncon Oct 04 '11 at 09:30