That date/time was generated from this code:
var now = new Date(); var utcDate = new Date( now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds() );
That looks correct to me at this point. I then need to take that string and convert it to a date/time in C#. In an attempt to do that, I'm using the following C# code:
DateTime _value = DateTime.MinValue; DateTime.TryParseExact(value, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out _value)
When that happens, I get the following date/time. 6/3/2012 12:30:43 PM
What am I doing wrong? I was expecting the date/time to be 6/3/2012 4:30:43 PM
The result you get is correct, but please check the
Kind property of your
DateTime. You'll notice it's not set to
UTC but to
You can use
DateTimeStyles.AdjustToUniversal to generate a
Kind set to
DateTime dateTime; DateTime.TryParseExact( value, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out dateTime);
See it working on ideone.
If you just want to serialize using (e.g. webAPI) I find JS
toISOString very useful and compatible.
That way you can keep your data structure and not deal with parsing.
You must pass date parameter to web service as UTC format and then use TryParse method to convert it to C# date object.
And this is C# code:
DateTime date; DateTime.TryParse(jsDateString, out date); //parsed as: '21.03.2013 14:44:40'
Hope this helps.
©2020 All rights reserved.