C# Epoch time to date and timezone convertion -
what way convert epoch time date, using specific timezone
i need check 2 dates. 1 date have in database , in x website.
i know both dates same in relation of yyyy-mm-dd
in example:
database date is: 1398891600 = 2014-05-01
x website date is: 1398902400000 = 2014-05-01
c# code "converter"
private string datechecker_inepoch(string epochdate) { if (epochdate.length < 13) { epochdate = epochdate + "000"; } datetime dt = new datetime(1970, 1, 1, 0, 0, 0).addmilliseconds(convert.todouble(epochdate)); string dateformated = dt.tostring("yyyy-mm-dd"); return dateformated; }
issue when convert using method 1 db date 2014-04-30 , website date 2014-05-01.
i tested dates using http://www.epochconverter.com/
database date is: 1398891600 = 2014-05-01
x website date is: 1398902400000 = 2014-05-01
epochconverter.com gives me following results:
1398891600000 -> wed, 30 apr 2014 21:00:00 gmt
1398902400000 -> thu, 01 may 2014 00:00:00 gmt
so code seems consistent epochconverter.com.
with regards timezones, make sure dates stored/retrieved using utc. save lot of headaches when comes converting to/from different timezones. in case, assuming epoch stored relative utc date, following:
datetime dt = new datetime(1970, 1, 1, 0, 0, 0, datetimekind.utc).addmilliseconds(convert.todouble(epochdate));
you can use following if need display local time end user:
utcdate.tolocaltime();
Comments
Post a Comment