There is a json in which there is a date and time of the beginning of a certain event, given by a number, for example 1461578400000. How can the start time be encrypted here? I do not understand, TimeSpan.FromMiliseconds() gives a strange result, DateTime.FromBinary() too. I can not figure it out
2 answers
What exactly JSON deserializer do you use?
In general, the date / time format in JSON is not standardized. Therefore, they differ in different serializers.
For example, System.Web.Script.Serialization.JavaScriptSerializer from the System.Web.Extensions library understands your values.
JavaScriptSerializer jss = new JavaScriptSerializer(); var dt1 = jss.Deserialize<DateTime>(@"""\/Date(1461578400000)\/"""); Console.WriteLine(dt1); // 25.04.2016 10:00:00 var dt2 = jss.Deserialize<DateTime>(@"""\/Date(1335205592410)\/"""); Console.WriteLine(dt2); // 23.04.2012 18:26:32 As noted by Yaant , this is the Unix timestamp. In .NET version 4.6, new methods have appeared, in particular, DateTimeOffset.FromUnixTimeSeconds .
var dto1 = DateTimeOffset.FromUnixTimeMilliseconds(1461578400000); var dto2 = DateTimeOffset.FromUnixTimeMilliseconds(1335205592410); This json is obviously generated in javascript, and includes milliseconds. Therefore, when converting, the date is obtained in the distant future.
In order to get the correct date, you need to divide the timestamp by 1000 and round up to a whole number to get the usual seconds.
- So it will turn out 405 hours ... so what? How can I get this out beautifully? It's a few days like ... so it’s necessary to count from some day? - Vladimir Paliukhovich
- Wait, what countdown, what hours, where to get it? Where did 405 come from? Timestamp is a timestamp, it is impossible to specify a period in it. - Alexey Ukolov
- if we divide this number by 1000 then by 60 then again by 60, we get 405 hours approximately ... Aa, it means that this is not an interval, but a mark. And how to translate it into a good presentation? - Vladimir Paliukhovich
- Of course, divide by 60 does not need anything. Regarding how to withdraw, I prompt, unfortunately I can not. Ask a separate question about this (just remember to indicate what meaning you put into the term "good presentation"). Better yet, just use a search engine, for sure you need something standard. - Alexey Ukolov
- It's clear that it's standard, but the problem is that I don't know what exactly is needed and how I can bring this long number to the form, for example 12:40 - Vladimir Paliukhovich
\/Date(1461578400000)\/? - Alexander Petrov1461578400000corresponds to the time25.04.2016 10:00:00.000 UTC, and1335205592410is23.04.2012 18:26:32.410. - Yaant