Home > Net >  convert to DateTime long int |.C#
convert to DateTime long int |.C#

Time:01-12

In my api response I have key such long int type.

{
  "1640908800000": 123945000000,
  "1648684800000": 97278000000,
  "1656547200000": 82959000000,
  "1664496000000": 90146000000,
  "index": "TableIndex"
},
{
  "1640908800000": 69702000000,
  "1648684800000": 54719000000,
  "1656547200000": 47074000000,
  "1664496000000": 52051000000,
  "index": "BookIndex"
}

1664496000000 key It must be DateTime. I didn't see such example. According my api and information from website it must be 9/29/2022

How I can parse it to normal DateTime Format?

CodePudding user response:

That looks like Unix timestamp so this is how you can convert it to datetime:

DateTimeOffset dateTime = DateTimeOffset.FromUnixTimeMilliseconds(timestamp);

CodePudding user response:

This seems to be Unix timestamp in milliseconds, to convert it to DateTime you need to parse it as long and then process for example with DateTimeOffset.FromUnixTimeMilliseconds:

DateTime dt = DateTimeOffset.FromUnixTimeMilliseconds(long.Parse("1664496000000"))
    .UtcDateTime;
Console.WriteLine(dt); // Prints "9/30/2022 12:00:00 AM"
  • Related