It shows that you are unregistered. Please register with us by clicking Here
![]() |
|
![]() |
![]() | Register - FAQ - Today's Posts - New Posts - Support - Search | ![]() |
|
|
#1 (permalink) |
|
Member
Join Date: Jul 2006
Age: 48
Posts: 45
|
This function compares two dates and returns the number of years,month and days between them as an array of three numbers:
function getTimeBetween(from, until) { var past = from == '' ? new Date() : new Date(from); var future = until == '' ? new Date() : new Date(until); if(past >= future) { var tmp = past; past = future; future = tmp; } var between = [ future.getFullYear() - past.getFullYear(), future.getMonth() - past.getMonth(), future.getDate() - past.getDate() ]; if(between[2] < 0) { between[1]--; var ynum = future.getFullYear(); var mlengths = [ 31, (ynum % 4 == 0 && ynum % 100 !== 0 || ynum % 400 == 0) ? 29 :28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]; var mnum = future.getMonth() -1; if(mnum < 0) {mnum += 12;} between[2] += mlengths[mnum]; } if ( between[1] < 0) { between[0]--; between[1] += 12; } return between; } To use the function, we specify a date in the past and a date in the future using a compatible format, such as "25 July, 2006", or "Mon, 25 July 2006 15:04:00 GMT+0100". The object also understands US time-zone abbreviations (such as PST), and assumes GMT if none is specified ( and midnight GMT if no time is specified). if you specify an emty string for either argument, the current date will be used. Here is how to use the function: var until = getTimeBetween('25 July 2006', '10 Jun 2014'); alart(until[0]+ 'years ' + until[1] + 'months ' + until[2] + 'days '); |
|
|
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Photoshop Seminar Tour dates | Evolution Graphics | Graphics & Multimedia | 10 | 10-12-2006 08:08 AM |
| My Exams dates have been changed. | Silent | All Things General | 4 | 03-09-2006 05:20 PM |