Answer the question
In order to leave comments, you need to log in
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
var dateTime = XmlConvert.ToDateTime(@"2014-06-18T20:00:00.000Z", XmlDateTimeSerializationMode.Utc);
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
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 questionAsk a Question
731 491 924 answers to any question