|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Javascript date comparison
Hi,
I have a form for user to apply for booking of place. The date of event (from text field) should be at least 7 days from the date of application. In other words, the user has to apply at least 7 days before the date of the event. here is the javascript //if the event date of the text field is not empty if(c.txtDate.value !="") { var myDate = c.txtDate.value; var myDate_array = myDate.split("-"); var dd = myDate_array[0]; var mth = myDate_array[1]; var yy = myDate_array[2]; var argumentDate = new Date(yy, mth, dd); //get the date 7 days before the event argumentDate.setDate(argumentDate.getDate()-7); var day = argumentDate.getDate(); var month = argumentDate.getMonth(); var year = argumentDate.getYear(); if (day<10) { day="0"+day; } if (month<10) { month="0"+month; } var limitDate = day + "-" + month + "-" + year; //get today's date to compare. var today = new Date(); var d = today.getDate(); var m = today.getMonth(); var y = today.getYear(); var todayDate = d + "-" + m + "-" + y; if (todayDate>limitDate) { alert ("The application date has to be at least 7 days before the event date"); return false; } } It validates correctly if the date entered is within this year. but if the user were to key in 2006, the validation return true. I have tried changing the date format to year-month-day, but the validation doesn't work as it return false even it is more than 7 days. How can I validate it? Thanks ! =) |
|
#2
|
||||
|
||||
|
convert both dates to timestamps and compare them that way....it is much quicker and perfectly simple.
__________________
Design first! Code later! |
|
#3
|
|||
|
|||
|
Thanks for your reply
I can actually convert the value in the text field to timestamp???
|
|
#4
|
||||
|
||||
|
yes, simply create a date object from the date string, then get dateObject.getTime()
where dateObject is the name of your date object
__________________
Design first! Code later! |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|