Hi All,
I have an int=4003; which supposed to represent days since 01/01/1998,how to convert that number of days to an actual date format mm/dd/yyyy. By the way the solution is 01/05/2009.
Any help is appreciated.
, Best regards
Hello and welcome
Edit: Actually it may still be helpful to you so I put it back:
A year is 365 days, except leap years with 366 days.
Here is a function to calculate if a given year is a leap year:
bool isLeapYear( const uint16_t year )
{
return ( (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0) );
}
Additionally, here is how to calculate number of days in months:
uint8_t getDaysInMonth( const uint8_t month, const uint16_t year )
{
if ( month == 2 )
return isLeapYear( year ) ? 29 : 28;
else if ( month == 4 || month == 6 || month == 9 || month == 11 )
return 30;
return 31;
}
@Delta_g:
use the time library to handle turning that time stamp not a date.
Can you please explain how to convert.
Or something like this:
To be honest, I'm not 100% sure it will always work (surely doesn't work for negative offsets), but I have tested a few times and gives correct solutions (by the way your solution is wrong).
Delta_G's solution is better anyway.
1/1/1998 is 883612800 as unix time. Add 4003*86400 to it = 1229472000
Use function breaktime(t, tm) from the Time library to convert this unix time into a TimeElements variable. Alternatively, you can use functions day(t), month(t) and year(t) (also from the Time library)
starwick:
@Delta_g:
use the time library to handle turning that time stamp not a date.
Can you please explain how to convert.
You populate a tmElements_t struct, and then apply makeTime().
guix:
Or something like this:
NNHaKn - Online C++ Compiler & Debugging Tool - Ideone.comTo be honest, I'm not 100% sure it will always work (surely doesn't work for negative offsets), but I have tested a few times and gives correct solutions (by the way your solution is wrong).
Delta_G's solution is better anyway.
1/1/1998 is 883612800 as unix time. Add 4003*86400 to it = 1229472000
Use function breaktime(t, tm) from the Time library to convert this unix time into a TimeElements variable. Alternatively, you can use functions day(t), month(t) and year(t) (also from the Time library)
I you are correct I'm using labview and I getting 12/16/2008 that sound correct??/
No it should be 12/17/2008, at least according to my code and these websites:
Online Conversion - Unix time conversion (enter 1229472000 in the field)
Thank you all for your help here is my working code converting days for 01/01/1998 to an actual date. so now the question is how to convert back.
//1/1/1998 is 883612800 as unix time. Add 4003*86400 to it = 1229472000
//Use function breaktime(t, tm) from the Time library to convert this unix time into a TimeElements variable. Alternatively, you can use functions day(t), month(t) and year(t) (also from the Time library)
#include <TimeLib.h>
long start_date = 883612800;
unsigned long offset_days = 4003;
unsigned long t_unix = 0;
void setup() {
Serial.begin(9600);
offset_days = offset_days * 86400;//convert number of days to seconds
Serial.println(offset_days);
t_unix = offset_days + start_date;
Serial.println(t_unix);
}
void loop() {
Serial.print("Date : ");
Serial.print(month(t_unix));
Serial.print("/");
Serial.print(day(t_unix));
Serial.print("/");
Serial.println(year(t_unix));
delay(2000);
}
let's say I have a date for the sake of this example 12/17/2008.
need to convert that to days since 01/01/1998(4003).
I do know the constant 01/01/1998 is 883612800 but how to convert 12/17/2008 to a number.
I see the function:
//
makeTime(tm);
Convert normal date & time to a time_t number. The time_t number is returned. The tm input is a TimeElements variable type, which has these fields:
tm.Second Seconds 0 to 59
tm.Minute Minutes 0 to 59
tm.Hour Hours 0 to 23
tm.Wday Week Day 0 to 6 (not needed for mktime)
tm.Day Day 1 to 31
tm.Month Month 1 to 12
tm.Year Year 0 to 99 (offset from 1970)
//
but how do I make tm
Thank you you all.
Thank you all;
I got it to work below is the code to convert days since 01/01/1998 to an actual date format mm/dd/yyyy. also I converted a date month1/day1/year1 to a uninx time then back to days from 01/01/1998.
You guys are awesome Thank you.
//1/1/1998 is 883612800 as unix time. Add 4003*86400 to it = 1229472000
//Use function breaktime(t, tm) from the Time library to convert this unix time into a TimeElements variable. Alternatively, you can use functions day(t), month(t) and year(t) (also from the Time library)
#include <TimeLib.h>
tmElements_t tmSet;
time_t tSet;
int month1 = 12;
int day1 = 17;
int year1 = 2008;
long start_date = 883612800;
unsigned long offset_days = 4003;
unsigned long t_unix = 0;
void setup() {
Serial.begin(9600);
offset_days = offset_days * 86400;//convert number of days to seconds
Serial.println(offset_days);
t_unix = offset_days + start_date;
Serial.println(t_unix);
tmSet.Year = year1-1970;
tmSet.Month = month1;
tmSet.Day = day1;
tSet = makeTime(tmSet);
Serial.print("tSet:");
Serial.println(tSet);
}
void loop() {
Serial.print("Date : ");
Serial.print(month(t_unix));
Serial.print("/");
Serial.print(day(t_unix));
Serial.print("/");
Serial.println(year(t_unix));
Serial.print("Date1 : ");
Serial.print(month(tSet));
Serial.print("/");
Serial.print(day(tSet));
Serial.print("/");
Serial.println(year(tSet));
long days_since = tSet - start_date;
days_since = days_since /86400;
Serial.print("Days Since:");
Serial.println(days_since);
delay(2000);
}