Working with dates from HTML forms
Working dates from text inputs is not an easy task due to the wide range of different notations and, paradoxically, the similarities between them.
For example, you can indicate if it is DD / MM / YYYY:
dateString = "24/03/1983";
But what about this? This is DD / MM / YYYY or MM / DD / YYYY:
dateString = "11/10/1998";
Let me tell you: it is impossible to guess . And you can be sure that even if you print a notification just above your field, whether it is highlighted in bold red flashing 28px Comic Sans MS, the user enters the date in the format in which it is used. And you cannot even blame him because you will do the same. This is just operant conditioning behavior .
That is why, throughout the network, most date entries are made through 3 drop-down lists: day, month, year.
We inform you whether the entry was in the past or not
As soon as you do this and consider your values:
var day = document.bookingsform.date.day.value var month = document.bookingsform.date.month.value var year = document.bookingsform.date.year.value
Now you can run your test:
inputTime = new Date(year + "/" + month + "/" + day).getTime(); // then you can do this: currentDate = new Date; currentDate.setHours(0); currentDate.setMinutes(0); currentDate.setSeconds(0); currentTime = currentDate.getTime(); notInPast = (inputTime - currentTime < 1000); // or this: currentTime = Math.round((new Date).getTime() / 86400000) * 86400000; notInPast = (inputTime >= currentTime);
Part with setHours() , etc. necessary because when you start a new Date object specified by DD / MM / YYYY, JavaScript will assume that the time for this Date is 00:00. While the time for the current Date (obtained using new Date ) is the current time.
So, if you do not perform such a correction, you will always indicate that DD / MM / YYYY is in the past today. Which you do not want.