ASP.net C # Parse int as datetime

Given the time:

1286294501433

What are the milliseconds passed since 1970, how do we convert this to a DateTime data type? EG:

transactionTime = "1286294501433";
UInt64 intTransTime = UInt64.Parse(transactionTime);
DateTime transactionActualDate = DateTime.Parse(intTransTime.ToString());

Throws:

The string was not recognized as a valid DateTime.

Please note that all time elapsed with this feature is guaranteed after 1970.

+3
source share
3 answers
var dt = new DateTime(1970, 1, 1).AddMilliseconds(1286294501433);

You may also need to explicitly specify DateTimeKind, depending on your exact requirements:

var dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
             .AddMilliseconds(1286294501433);
+14
source

And simplify it, and also consider your local time zone:

Just create this integer -

  public static class currency_helpers {
    public static DateTime UNIXTimeToDateTime(this int unix_time) {
      return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(unix_time).ToLocalTime();
    }
  }

And then call it anywhere:

var unix_time = 1336489253;    
var date_time = unix_time.UNIXTimeToDateTime();

Value date_time:

5/8/2012 10:00:53 AM

(: http://www.codeproject.com/Articles/10081/UNIX-timestamp-to-System-DateTime?msg=2494329#xx2494329xx)

+1

, unix time, ,

int unixtimestamp=int.Parse(str);
new DateTime(1970,1,1,0,0,0).AddSeconds(unixtimestamp);

as this bait guy said .

wiki says

Unix time or POSIX time is a system for describing points in time, defined as the number of seconds elapsed from the midnight proleptic Coordinated Universal Time (UTC) of January 1, 1970, not counting the seconds of the jump. I

0
source

Source: https://habr.com/ru/post/1768201/


All Articles