c# - How to convert a Unix timestamp to DateTime and vice versa? -
there example code, starts talking millisecond / nanosecond problems.
the same question on msdn, seconds since unix epoch in c#.
this i've got far:
public double createdepoch { { datetime epoch = new datetime(1970, 1, 1, 0, 0, 0, 0).tolocaltime(); timespan span = (this.created.tolocaltime() - epoch); return span.totalseconds; } set { datetime epoch = new datetime(1970, 1, 1, 0, 0, 0, 0).tolocaltime(); this.created = epoch.addseconds(value); } }
here's need:
public static datetime unixtimestamptodatetime( double unixtimestamp ) { // unix timestamp seconds past epoch system.datetime dtdatetime = new datetime(1970,1,1,0,0,0,0,system.datetimekind.utc); dtdatetime = dtdatetime.addseconds( unixtimestamp ).tolocaltime(); return dtdatetime; }
or, java (which different because timestamp in milliseconds, not seconds):
public static datetime javatimestamptodatetime( double javatimestamp ) { // java timestamp milliseconds past epoch system.datetime dtdatetime = new datetime(1970,1,1,0,0,0,0,system.datetimekind.utc); dtdatetime = dtdatetime.addmilliseconds( javatimestamp ).tolocaltime(); return dtdatetime; }
Comments
Post a Comment