0

can not convert date The following object to datetime in c#

{"date":1606813200000 , "open":119000.0, "high":130900.0, "low":107500.0, "close":113300.0, "volume":36892044.0}

I do not understand the relationship between the two

Why 1606813200000 == 2020-12-01T09:00:00.000Z?

can you cee , ralationship to site https://jsoneditoronline.org/

bolov
  • 72,283
  • 15
  • 145
  • 224
  • 1
    `string isoDate = DateTimeOffset.FromUnixTimeMilliseconds(1606813200000).ToString("o");`? – ProgrammingLlama Jul 26 '21 at 03:18
  • `1606813200000` is the time in milliseconds between `1970-01-01T00:00:00.000Z` (the "unix epoch") and `2020-12-01T09:00:00.000Z`. You can see this by entering it [here](https://www.epochconverter.com/). – ProgrammingLlama Jul 26 '21 at 03:19
  • 1
    Does this answer your question [Convert epoch/unix to Datetime](https://stackoverflow.com/questions/15926261/convert-epoch-unix-to-datetime) and [C# Epoch time to date and timezone convertion](https://stackoverflow.com/questions/29862301/c-sharp-epoch-time-to-date-and-timezone-convertion) –  Jul 26 '21 at 03:21

1 Answers1

2

The number you see is a Javascript timestamp (number of milliseconds elapsed since January 1, 1970 00:00:00 UTC). You can test it in any browser console:

enter image description here

Following this article, you can convert back to DateTime object:

public static DateTime ConvertFromUnixTimestamp(double timestamp)
{
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    return origin.AddSeconds(timestamp / 1000); // convert from milliseconds to seconds
}

From the comment, I realize there is this method DateTimeOffset.FromUnixTimeMilliseconds as well:

// Convert Timestampt to DateTimeOffset
var time = DateTimeOffset.FromUnixTimeMilliseconds(1606813200000);

// Convert back to ISO string
var isoString = time.ToString("o");
Luke Vo
  • 17,859
  • 21
  • 105
  • 181