32

I have javascript date object which gives me a date string in this format, "Wed Dec 16 00:00:00 UTC-0400 2009".

I pass this via Ajax to the server (ASP.NET c#)

How can I convert, "Wed Dec 16 00:00:00 UTC-0400 2009" to a C# DateTime object. DateTime.Parse fails.

skaffman
404k96 gold badges824 silver badges775 bronze badges
asked Dec 9, 2009 at 23:51

5 Answers 5

54

You can use DateTime.ParseExact which allows you to specify a format string to be used for parsing:

DateTime dt = DateTime.ParseExact("Wed Dec 16 00:00:00 UTC-0400 2009",
 "ddd MMM d HH:mm:ss UTCzzzzz yyyy",
 CultureInfo.InvariantCulture);
answered Dec 9, 2009 at 23:58

1 Comment

You may want to use DateTimeOffset instead.
32

The most reliable way would be to use milliseconds since the epoch. You can easily get this in JavaScript by calling Date.getTime(). Then, in C# you can convert it to a DateTime like this:

long msSinceEpoch = 1260402952906; // Value from Date.getTime() in JavaScript
return new DateTime(1970, 1, 1).AddTicks(msSinceEpoch * 10000);

You have to multiply by 10,000 to convert from milliseconds to "ticks", which are 100 nanoseconds.

answered Dec 10, 2009 at 0:03

6 Comments

I know this is an old, old answer but I do have a question. Is there any particular reason you add a new TimeSpan instead of calling DateTime's .AddMilliseconds method?
No reason I can remember. I've edited the answer to use AddTicks now, which is probably marginally faster than AddMilliseconds (we're talking a few CPU cycles here).
I got curious about why the "epoch" is 1970年01月01日. It turns out this is the "Unix epoch." I thought I'd share in case others are curious or confused: en.wikipedia.org/wiki/Unix_time
Any reason not to just use DateTime.AddMilliseconds? return new DateTime(1970, 1, 1).AddMilliseconds(msSinceEpoch);
Pay attention: JS Date.getTime returns the time in UTC. so if you want local time in C# you have to use ToLocalTime(): return new DateTime(1970, 1, 1).AddTicks(msSinceEpoch * 10000).ToLocalTime();
|
22

This may not be possible in your case, but I really recommend updating the JS code to pass dates/times in ISO 8601 format. http://en.wikipedia.org/wiki/ISO_8601

ISO 8601 is not only the formal standard, it's also easy to use and prevents a lot of timezone hassle!

To get 8601 datetime strings in Javascript:

var d = new Date();
var iso_time = d.toISOString(); //"2014-05-06T18:49:16.029Z"

To read 8601 datetime strings in C#:

DateTime d = DateTime.Parse(json_string);
answered May 6, 2014 at 18:58

2 Comments

I see this as being the best approach because it's a single step and doesn't require jumping through any other hoops to handle the timezone or System.DateTime parsing.
I'm using this approach to conform to momentjs
5

Just for posterity, to help future fellow Googlers, I'd like to expand on EMP's answer.

EMP's answer provides the time in UTC (if that's what you're looking for, use that).

To arrive at the client local time in C#:

In JavaScript:

 var now = new Date();
 var UTC = now.getTime();
 var localOffset = (-1) * now.getTimezoneOffset() * 60000;
 var currentTime = Math.round(new Date(UTC + localOffset).getTime()); 

In C#:

 DateTime currentTimeDotNet = new DateTime(1970, 1, 1).AddTicks(Convert.ToInt64(currentTime) * 10000);

Credit to this blog and EMP's answer, but took some trial and error on both ends to get it right, so just fyi for future folks.

answered Oct 19, 2012 at 22:39

1 Comment

This is the most-correct answer that correctly handles time-zone adjustment.
0

To be honest I wouldn't try to parse that date string in C#, I'd personally try to create a more useful date structure from your javascript date object.

For instance you could use parse() in javascript which will return the ms representing the date object, which you can use DateTime.Parse() on to convert into a C# DateTime object.

answered Dec 9, 2009 at 23:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.