-1

I need a user input for 2 dates, and get the difference between them, and if it is more than 14 days, throw an error. Help appreciated
The date is in the format yyyy/mm/dd

Also, I want to match the pattern of the date to check if it is valid, I have done everything, month and year, except the day, it accepts days from 32-39 too, what should I do to correct it?

sid0972
  • 53
  • 2
  • 7

2 Answers2

2

Assuming date1 and date2 are both Date Objects. Here's how you'd find if there was a 14 day difference between the two.

Math.abs(+date1 - (+date2)) > 12096000000

There are 12096000000 milliseconds in 14 days.

14 days x 24 hours x 60 minutes x 60 seconds x 1000 ms

There's an SO question which addresses validating dates from user inputs here.

Community
  • 1
  • 1
John Strickler
  • 25,151
  • 4
  • 52
  • 68
  • i am not sure if they are plain javascript objects, however, i have defined them as date1=newDate and date2=new Date, and thanx for quick reply – sid0972 Dec 02 '11 at 13:40
  • Yea those are plain date object - didn't mean to cause confusion. – John Strickler Dec 02 '11 at 13:44
  • i am getting the input using dicument.getelementbyid, and the link that you have shown doesnt take it that way,it already has then input – sid0972 Dec 02 '11 at 13:51
1

For two dates dateA and dateB, the difference in days in Javascript is:

daysDifference = Math.round((dateA - dateB)/(1000*60*60*24));

Check the jsFiddle for a working example.

Kostas Maragos
  • 569
  • 4
  • 18
  • i am getting the input from input boxes and i am trying to find a code for perl script as it goes to ny database – sid0972 Dec 02 '11 at 15:11