Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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.
 
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:
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?

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:
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.

Latest threads

New Articles From Microcontroller Tips

Back
Top