# Days difference between 2 Time-Stamps.

Status
Not open for further replies.

#### alphacat

##### New Member
Hello guys,

I'm having difficulties with finding the days difference between 2 time-stamps.

Each time stamp is given by a struct:

unsigned short year; (2 bytes)
unsigned char month; (1 byte)
unsigned char day;
unsigned char hour;
unsigned char min;
unsigned char sec;

Given two time-stamps, how can I know what is the days difference between these two? (considering 28/29/30/31 monthes as you're familiar with).

For example, days difference between 22.1.09 and 23.1.09 is 1 day, no matter what is the hours/mins/sec/ differences.

Thank you very much for any help.

#### Nigel Goodwin

##### Super Moderator
Try looking up Julian numbers, by converting the year/month/day to a Julian number you can simply do maths on dates.

#### alphacat

##### New Member
I used the forumula written in wikipedia, the one of JD, and it miscalculates when I enter two days in different monthes, for example I entered (in format of m/d/y h:min:sec)
Time-stamp_1 = 2.28.2009, 00:00:00.
Time-stamp_2 = 3.1.2009, 00:00:00.
and TS2 - TS1 gave me 4.
You have any idea why?

Last edited:

#### Nigel Goodwin

##### Super Moderator
I've no idea what Wikipedia says, I wrote my own routines years ago, in Turbo Pascal running under DOS, way back before Windows.

#### alphacat

##### New Member
Thanks.
Do you know another method for calculating days interval between two time-stamps?

#### Nigel Goodwin

##### Super Moderator
Thanks.
Do you know another method for calculating days interval between two time-stamps?
No, you need to convert them both to numbers, then do simple maths - this is how dates are stored on your PC and on the net.

#### David James

##### New Member
I used the forumula written in wikipedia, the one of JD, and it miscalculates when I enter two days in different monthes, for example I entered (in format of m/d/y h:min:sec)
Time-stamp_1 = 2.28.2009, 00:00:00.
Time-stamp_2 = 3.1.2009, 00:00:00.
and TS2 - TS1 gave me 4.
You have any idea why?
What numbers do you get for the JDN for your two timestamps?

Julian Day Number Calculations

gives:

2009-02-28 = JDN 2454891

2009-03-01 = JDN 2454892

#### alphacat

##### New Member
I receive: 2009-02-28 = JDN 2454890.94
and: 2009-03-01 = JDN 2454894.376875

#### alphacat

##### New Member
They dont explain in the link you gave how they calculate it, so its not so helpful.

#### David James

##### New Member
They dont explain in the link you gave how they calculate it, so its not so helpful.
It's Javascript - look at the source of the page.

#### David James

##### New Member
I receive: 2009-02-28 = JDN 2454890.94
and: 2009-03-01 = JDN 2454894.376875
There's clearly something wrong with your calculation - you shouldn't have fractional parts like that if the time is 00:00:00.

#### alphacat

##### New Member
Here is my calculation:
double JdnCalc(int day,int month,int year,int hour,int minute, int second)
{
double JD, JDN, a, y, m;

a = (14 - month)/12.0;
y = year + 4800 - a;
m = month + 12*a -3;
JDN = day + (153*m +2)/5.0 + 365*y + y/4.0 -y/100.0 + y/400.0 - 32045;
JD = (int)(JDN) + (hour-12)/24.0 + minute/1440.0 + second / 86400.0;
return(JD);
}

Whats wrong with it?

Last edited:

#### David James

##### New Member
Here is my calculation:
double JdnCalc(int day,int month,int year,int hour,int minute, int second)
{
double JD, JDN, a, y, m;

a = (14 - month)/12.0;
y = year + 4800 - a;
m = month + 12*a -3;
JDN = day + (153*m +2)/5.0 + 365*y + y/4.0 -y/100.0 + y/400.0 - 32045;
JD = (int)(JDN) + (hour-12)/24.0 + minute/1440.0 + second / 86400.0;
return(JD);
}

Whats wrong with it?
You've missed out the floor functions (those are the square brackets with the top horizontal line missing).

Try:

Code:
double JdnCalc(int day,int month,int year,int hour,int minute, int second)
{
double JD, JDN, a, y, m;

a = [B]floor[/B]((14 - month)/12.0);
y = year + 4800 - a;
m = month + 12*a -3;
JDN = day + [B]floor[/B]((153*m +2)/5.0) + 365*y + [B]floor[/B](y/4.0) - [B]floor[/B](y/100.0) + [B]floor[/B](y/400.0) - 32045;
JD = (int)(JDN) + (hour-12)/24 + minute/1440 + second / 86400;
return(JD);
}

Last edited:
Status
Not open for further replies. 