Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

c# - DateTime.ParseExact format string

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...