Contact   •   Products   •   Search

Rick Strahl's Web Log

Wind, waves, code and everything in between...
ASP.NET • C# • HTML5 • JavaScript • AngularJs

Turn a JavaScript Date into a .NET DateTime value


Argh… this is annoying. I was mucking around with a Callback function in Anthem that's sending back a date from JavaScript. Anthem allows making JSON page method calls to the server, but it doesn't provide two way JSON support so any data passed from the client passes values as strings.

 

Well, dates come back in a really fucked up toString() format that is a pain to parse.

 

JavaScript has Date.toUTCString() and Date.toGTMString(), but these are returning different things for different browsers. IE basically returns UTC strings while Mozilla returns GMT strings – the only difference between the two really is the UTC or GMT designator.

 

So on the client I ended up with this code:

 

Anthem_InvokePageMethod('RefreshMessages',

                        [ChatId,LastOn.toUTCString()],

RefreshMessagesCallback);

 

Which in turns calls a method that looks like this:

 

public string RefreshMessages(string ChatId, string LastOn)

{

    // *** IE returns UTC - Mozilla returns GMT

    LastOn.Replace("UTC", "GMT");

 

    string[] fmt = {"r","R","ddd, dd MMM yyyy HH:mm:ss"};

    DateTime Time;

 

    if (!DateTime.TryParseExact(LastOn, fmt, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out Time))

        return "";

 

    string MessagesHtml = Message.GetMessagesHtml(Time, this.txtChatId.Text);

    return MessagesHtml;

}

 

The r format allows for GMT formatting and it looks like it works. I don't think it works for date time strings that have timezone offsets, but the toUTCString() function doesn't set that luckily.

 

This is one place where Anthem is lacking a little. A two way JSON parser would really make life easier. I've built a beginning parser into wwHoverPanel and it skirts this issue.

 

This is a little thing but I wasted probably a half an hour on this plus now another 10 minutes on this post <g>... Hope this helps somebody out.

 

Make Donation


Feedback for this Post

 
# re: Turn a JavaScript Date into a .NET DateTime value
by Bertrand Le Roy March 30, 2006 @ 9:31am
... Look at the Atlas converters. We have reliable two-way JSON serialization of dates...
# re: Turn a JavaScript Date into a .NET DateTime value
by Rick Strahl April 12, 2006 @ 12:44pm
Well that doesn't work quite like that because ATLAS uses its own date logic for encoding a DateTime value from the client through its JSON parser.


# DotNetSlackers: Turn a JavaScript Date into a .NET DateTime value
by DotNetSlackers Latest ASP.NET News October 25, 2006 @ 4:33am
# re: Turn a JavaScript Date into a .NET DateTime value
by CB January 12, 2011 @ 6:38am
This sample is quite helpful. However there is a small bug in the format string:
Correct is:
string[] fmt = {"r","R","ddd, d MMM yyyy HH:mm:ss"};

For day numbers between 1 and 9 the original format would fail because "dd" is not an exact match.
 


West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2014