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.
 
Try looking up Julian numbers, by converting the year/month/day to a Julian number you can simply do maths on dates.
 
Thank you for your answer.
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:
I've no idea what Wikipedia says, I wrote my own routines years ago, in Turbo Pascal running under DOS, way back before Windows.
 

What numbers do you get for the JDN for your two timestamps?

**broken link removed**

gives:

2009-02-28 = JDN 2454891

2009-03-01 = JDN 2454892
 
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:

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.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…