Conversion from milliseconds to DateTime format

后端 未结 3 952
面向向阳花
面向向阳花 2020-12-01 09:00

I got a string which is representend like this :

string startdatetime = \"13988110600000\"

What I want to do is to convert this string (wh

3条回答
  •  旧巷少年郎
    2020-12-01 09:34

    DateTime in .NET is initialized to 0001-01-01 00:00:00 and then you add your TimeSpan, which seems to be 45 Years.

    It is common for such (milli)-second time definitions to start at 1970-01-01 00:00:00, so maybe the following gives you the expected result:

    double ticks = double.Parse(startdatetime);
    TimeSpan time = TimeSpan.FromMilliseconds(ticks);
    DateTime startdate = new DateTime(1970, 1, 1) + time;
    

    or simply

    var date = (new DateTime(1970, 1, 1)).AddMilliseconds(double.Parse(startdatetime));
    

提交回复
热议问题