How can I validate in Javascript that two dates are within the same month without comparing the first two characters of the date string (i.e. 05/11/2004)?
Printable View
How can I validate in Javascript that two dates are within the same month without comparing the first two characters of the date string (i.e. 05/11/2004)?
what form is the input in?
if it is dd/mm/yyyy or mm/dd/yyyy, then you have to compare the mm to the other mm. Of course there are more and less efficient ways to do this.
Here's how I'd do it.
Code:date1 = "24/02/1986"
date2 = "11/05/2004"
Ar1 = new Array()
Ar2 = new Array()
Ar1 = split(date1,"/")
Ar2 = split(date2,"/")
if (Ar1[1] == Ar2[1])
{
//Same month
}
Dredging up a memory of javascript, there is a date type. If you can get the entered date into it I think there is a property or method of .month()
Vince
That will get you the current month, but this isn't very useful for comparing two dates. It could be if one of the dates is always todays date I suppose. You'll still have to use the mm from dd/mm/yyyy from the other date though.Code://To get the current month:
now = new Dat()
current_month = now.getMonth()
If you're simply trying to compare two dates, use the code I supplied in the first post.
Following on...Quote:
Code://To get the current month:
now = new Dat()
current_month = now.getMonth()
But hey its your choice, we are just providing options :)Code:strDate1 = "24/02/1986"
strDate2 = "11/05/2004"
// dunno if this works? or how it copes with american/english date formats
dte1 = strDate1.toDate()
dte2 = strDate2.toDate()
if (dte1.getMonth() == dte2.getMonth()) {
//do whatever
}
Vince