简体   繁体   English

DateTime.ParseExact格式字符串

[英]DateTime.ParseExact format string

I have a web application that passes a DateTime from one page to another through the query string. 我有一个Web应用程序,它通过查询字符串将DateTime从一页传递到另一页。 It was working just fine in both IE and FireFox, but was throwing exceptions whenever I tried it in Google Chrome. 它在IE和FireFox中都可以正常工作,但是每当我在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. 我得出的结论是,Convert不能胜任工作,并着手尝试使DateTime.ParseExact驯服此野兽。 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). 此页面是通过第三方组件(DayPilotCalendar)调用的某些JavaScript从另一页面调用的。 Here is the relevant property that is set on the DayPilotCalendar control: 这是在DayPilotCalendar控件上设置的相关属性:

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

What is wrong with my format string? 我的格式字符串有什么问题?

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? 我建议不要在查询字符串中传递类似“ Wed Oct 03 2012 08:00:00 GMT-0400(Eastn Daylight Time)”的信息,而是传递日期的时间戳? Eg, new Date().getTime(). 例如,新的Date()。getTime()。 (Number of milliseconds since 1970 in UTC). (自1970年以来,以UTC为单位的毫秒数)。 Then, in C# you could just do: 然后,在C#中,您可以执行以下操作:

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

No parsing required. 无需解析。

It may just be that your format doesn't cover the (Eastern Daylight Time) section. 可能是您的格式未涵盖(Eastern Daylight Time)部分。 Try parsing that out of your string using regular string handling methods, then calling ParseExact on the remainder. 尝试使用常规的字符串处理方法从字符串中解析出该字符串,然后在其余部分上调用ParseExact

Edit: As Oded points out, you'll also have to put the GMT into your format string as a literal: 编辑:正如Oded指出的那样,您还必须将GMT作为文字添加到格式字符串中:

"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);

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 2012年10月1日星期一10:52:20 -04:00

So I guess you need to parse the GMT and (Eastern Daylight Time) part of strings as well 所以我想您也需要解析字符串的GMT和(Eastn Daylight Time)部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM