javascript validation for dates takes string for comparison
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 sub开发者_如何学Pythonmit 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?
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){
....
<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.
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.
精彩评论