|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Calculating difference between two dates in javascript
Hi there,
I'm having some problems with calculating the difference between two dates. This following function can work on two dates within the same month but it doesn't work on two dates on different months:- firstDate = new Date(year1, month1, day1); secondDate = new Date(year2, month2, day2); msPerDay = 24 * 60 * 60 * 1000 dbd = Math.floor((secondDate-firstDate)/ msPerDay) + 1; Pls help... I have been thinking of solutions to solve that frustrating problem for some months already. ~:p Thanks so very much if you can help! [edit: moved by notepad] |
|
#2
|
|||
|
|||
|
RE: Calculating difference between two dates in javascript
Try converting each date into the number of seconds since an arbitrary starting point (they usually use Jan 1, 1970), subtracting the larger number (more recent date) from the smaller number) and then convert that into whatever time quantity you need.
|
|
#3
|
|||
|
|||
|
RE: Calculating difference between two dates in javascript
Hi filefrog,
thanks fr helping and i have solved the problem already. |
|
#4
|
|||
|
|||
|
RE: Calculating difference between two dates in javascript
cool. how didja do it (just curious, and also in case anyone else is facing the same 'problem') ?
|
|
#5
|
|||
|
|||
|
RE: Calculating difference between two dates in javascript
Hi FileFrog,
Here's the solution that i have managed to figured out: var day1, day2; var month1, month2; var year1, year2; value1 = form.datebox.value; value2 = form.datebox2.value; day1 = value1.substring (0, value1.indexOf ("/")); month1 = value1.substring (value1.indexOf ("/")+1, value1.lastIndexOf ("/")); year1 = value1.substring (value1.lastIndexOf ("/")+1, value1.length); day2 = value2.substring (0, value2.indexOf ("/")); month2 = value2.substring (value2.indexOf ("/")+1, value2.lastIndexOf ("/")); year2 = value2.substring (value2.lastIndexOf ("/")+1, value2.length); date1 = year1+"/"+month1+"/"+day1; date2 = year2+"/"+month2+"/"+day2; firstDate = Date.parse(date1) secondDate= Date.parse(date2) msPerDay = 24 * 60 * 60 * 1000 dbd = Math.round((secondDate.valueOf()-firstDate.valueOf())/ msPerDay) + 1; Hope this helps!! Sorry i was busy that didn't notice your next question.. :p |
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Client Side Things > Calculating difference between two dates in javascript |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|