Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
West Wind WebSurge - Rest Client and Http Load Testing for Windows

Turn a JavaScript Date into a .NET DateTime value


:P
On this page:

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.

 


The Voices of Reason


 

Bertrand Le Roy
March 30, 2006

# re: Turn a JavaScript Date into a .NET DateTime value

... Look at the Atlas converters. We have reliable two-way JSON serialization of dates...

Rick Strahl
April 12, 2006

# re: Turn a JavaScript Date into a .NET DateTime value

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


CB
January 12, 2011

# re: Turn a JavaScript Date into a .NET DateTime value

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 - 2024