T
T
TekVanDo2014-06-25 13:21:00
JavaScript
TekVanDo, 2014-06-25 13:21:00

How to cast Date object js to DateTime C# format?

I use api cotroller, it receives "2014-06-18T20:00:00.000Z" in json, which in js == Date {Thu Jun 19 2014 00:00:00 GMT+0400}.
C# after parsing json DateTime is 2014-06-18 instead of 2014-06-19.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Mikhail Andreev, 2014-06-25
@Mikant

var dateTime = XmlConvert.ToDateTime(@"2014-06-18T20:00:00.000Z", XmlDateTimeSerializationMode.Utc);

S
Sergey, 2014-06-25
Protko @Fesor

It's all about the time zone. Exchange dates in UTC.

S
solowk, 2014-11-21
@solowk

If no time component is needed, then the best option is:
JS:
date.toDateString(); // then this value can be parsed in Mono
C#:
DateTime dateFromJS = DateTime.Parse(js_date); // receive data
string dateToJS = DateTime.Now.ToLongDateString(); // send data

E
eRKa, 2018-11-03
@kttotto

The date comes to you taking into account the time zone. The DateTime type does not take into account the time zone, which means you will waste time when casting. If the time depends on the locale and the time zone is needed, then you need to accept the time in the DateTimeOffset format (well, or lead to it if you accept a string), then time will not be lost.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question