Comparing Two Dates - preCharge Forums
It shows that you are unregistered. Please register with us by clicking Here
preCharge Forums


Nav Green LeftNav Right
preCharge Forums > Website Design & Development > Programming > JavaScript » Comparing Two Dates


Reply
Tcat Right
 
LinkBack Thread Tools Display Modes Tcat Right
Old 07-25-2006   #1 (permalink)
Nazir
Member
 
Join Date: Jul 2006
Age: 48
Posts: 45
Default Comparing Two Dates

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 ');
Nazir is offline   Reply With Quote


Old 10-12-2006   #2 (permalink)
ScottHughes
Senior Member
 
Join Date: Sep 2006
Age: 22
Posts: 460
Default Re: Comparing Two Dates

Great code, you don't mind if I use that right?

Did you write it yourself?
__________________
ScottHughes is offline   Reply With Quote

Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


footer left
All times are GMT. The time now is 07:53 PM.

DISCLAIMER: preCharge Risk Management is not responsible for any opinions, advice or comments expressed on the preCharge Community Forums.
preCharge® is a registered trademark of preCharge Risk Management | chargeback protection | Merchant Account Blog

Powered by vBulletin
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC6

Home cinema sound system | Free Ringtones | Debt Management | Mortgage Calculator | Property in Spain

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49