2

I have a web application that passes a DateTime from one page to another through the query string. It was working just fine in both IE and FireFox, but was throwing exceptions whenever I tried it in Google Chrome. The program is choking on the following line:

startDateTime = Convert.ToDateTime(Request.QueryString["start"]);

So, I ran the debugger and found that the value in the query string is:

Wed Oct 03 2012 08:00:00 GMT-0400 (Eastern Daylight Time)

I concluded that Convert just wasn't up to the job and set about trying to get DateTime.ParseExact to tame this beast. But, so far the correct format string has eluded me. Here's the code that I've been trying (which doesn't work):

DateTime.ParseExact(Request.QueryString["start"], "ddd MMM dd yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture);

This page is being called from another page through some JavaScript that is called by a third-party component (DayPilotCalendar). Here is the relevant property that is set on the DayPilotCalendar control:

TimeRangeSelectedJavaScript="GB_showPage('Request Magnet Time', '../../../EventAddEdit.aspx?start=' + encodeURIComponent(start) + '&end=' + encodeURIComponent(end))"

What is wrong with my format string?

8
  • 1
    What format to you want the result string to be in? ie. 09/12/2012 or Sept 12 2012 etc. Commented Oct 1, 2012 at 14:40
  • 2
    @JordanKaye, the result should be a DateTime instance, not string. So there's really no format involved for the output. Commented Oct 1, 2012 at 14:40
  • I'm trying to get it back as a DateTime. Commented Oct 1, 2012 at 14:41
  • 2
    This doesn't seem to be any of the standard formats. Can you change the caller of your page to use some of the standard formats? Such as ISO 8601 for example. Commented Oct 1, 2012 at 14:49
  • 1
    Is the "Wed Oct 03 2012 08:00:00 GMT-0400 (Eastern Daylight Time)" from JS or from C#? Commented Oct 1, 2012 at 14:58

3 Answers 3

4

Might I suggest that instead of passing something like: "Wed Oct 03 2012 08:00:00 GMT-0400 (Eastern Daylight Time)" in your query string, that instead you simply pass the timestamp of the date? E.g., new Date().getTime(). (Number of milliseconds since 1970 in UTC). Then, in C# you could just do:

var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var dt =  epoch.AddMilliseconds(Convert.ToInt64(Request.QueryString["start"]));

No parsing required.

Sign up to request clarification or add additional context in comments.

Comments

3

It may just be that your format doesn't cover the (Eastern Daylight Time) section. Try parsing that out of your string using regular string handling methods, then calling ParseExact on the remainder.

Edit: As Oded points out, you'll also have to put the GMT into your format string as a literal:

"ddd MMM dd yyyy HH:mm:ss 'GMT'zzz"

The following works:

var input = "Wed Oct 03 2012 08:00:00 GMT-0400 (Eastern Daylight Time)";
var trim = input.Substring(0, input.IndexOf(" ("));
var dt = DateTime.ParseExact(
    trim,
    "ddd MMM dd yyyy HH:mm:ss 'GMT'zzz",
    CultureInfo.InvariantCulture);

3 Comments

The GMT will give problems as well.
Sorry to reneg on the accept for your answer. Your code works, but I ended up going with the solution suggested by aquinas, as it seems that it is likely to be more robust.
Well yes, if you've got control of what's generating the string, that makes much more sense...
0

I tried running the code

static void Main(string[] args) {
    Console.WriteLine(DateTime.Now.ToString("ddd MMM dd yyyy HH:mm:ss zzz"));            
    Console.Read();
}

Output is :

Mon Oct 01 2012 10:52:20 -04:00

So I guess you need to parse the GMT and (Eastern Daylight Time) part of strings as well

Comments

Your Answer

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.