BikerP
BikerP

Reputation: 868

Parse JS date to C# DateTime

I have javascript date object which gives me a date string in this format, "Tue Sep 04 2012B0100 (GMT Daylight Time)"

I am trying to parse with ParseEaxcat as mentioned here, but it throws an invalid date exception - anyone point me in the direction of the right format

                string date = "Tue Sep 04 2012B0100 (GMT Daylight Time)";
                dt = DateTime.ParseExact(date,"ddd MMM dd yyyyBzzzz",
                     CultureInfo.InvariantCulture);

I've also looked at this with no joy: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Upvotes: 3

Views: 7616

Answers (6)

tpayne84
tpayne84

Reputation: 192

There are a lot of ways to do this... But this is what I find to be the simplest...

// JavaScript
var d = new Date();
d.toLocaleString();
// =>   "6/26/2015, 2:07:25 PM"

// Can be Parsed by the C# DateTime Class
DateTime d = DateTime.Parse( @"6/26/2015, 2:07:25 PM" );
Console.WriteLine( d.ToLongDateString() );
// =>   Friday, June 26, 2015

Upvotes: 0

What Would Be Cool
What Would Be Cool

Reputation: 6758

I'm getting a different date time format from JavaScript. Here is what I had to do:

public void Main()
{
    Console.WriteLine(
        ConvertJsDate("Fri Apr 18 2014 16:23:18 GMT-0500 (Central Daylight Time)"));
    //test more regular date
    Console.WriteLine(
        ConvertJsDate("4/18/2014 16:23:18")); 
}

public DateTime ConvertJsDate(string jsDate)
{
    string formatString = "ddd MMM d yyyy HH:mm:ss";

    var gmtIndex = jsDate.IndexOf(" GMT");
    if (gmtIndex > -1) 
    {
        jsDate = jsDate.Remove(gmtIndex);
        return DateTime.ParseExact(jsDate, formatString, null);
    }
    return DateTime.Parse(jsDate);
}

Upvotes: 3

J. Tanner
J. Tanner

Reputation: 575

This works. Though, you may want to strip the GMT Daylight Time portion out before passing it in just to avoid the in-line split.

string date = "Tue Sep 04 2012B0100 (GMT Daylight Time)";
var dt = DateTime.ParseExact(date.Split('(')[0].Replace("B","+").Trim(), "ddd MMM dd yyyyzzz", CultureInfo.InvariantCulture);

Edited to account for the offset.

Upvotes: 3

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

Alternative approach is to convert date into reasonable representation on JavaScript side will be significantly more robust: no need to guess language server side, may handle timezones correctly.

If you use some sort of automated conversion (i.e. JSON.stringify) you may need to add one more field parallel to your date field with string representation of the same value and use it on server side instead of original one.

{ dateFied: new Date(),
  dateFiledAsIsoString: "....." }

If decided to go this route consider also passing Time Zone (time offset) to server side code or converting time to UTC on JavaScript side. Consider using ISO8601 format for date: yyyy-MM-ddTHH:mm:ss.fffffffzzz.

Upvotes: 0

djs
djs

Reputation: 230

The date doesn't appear to match the format string. The format string has hyphens, and is missing the parenthesized section. Also, there is no mention of a format string with 4 z's, so you might change the first one to a 0.

Upvotes: 0

PPC-Coder
PPC-Coder

Reputation: 3632

If you can (and it sounds like you do since you have the object), I'd recommend extracting the number of milliseconds since 1970/01/01 from Javascript (.getTime()), convert it to .Net ticks (100-nanosecond units), and use that to parse into a C# DateTime.

var ticks = (jsMillis * 10000) + 621355968000000000;
var date = new DateTime(ticks);

where jsMillis is the number you get from calling getTime() on the Javascript DateTime object.

The 621355968000000000 is to convert from C#'s date origin (Midnight Jan 1, 0001) to javascript's date origin.

Upvotes: 8

Related Questions