DateTime.ParseExact格式字符串 [英] DateTime.ParseExact format string

查看:184
本文介绍了DateTime.ParseExact格式字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过从一个页面一个DateTime到另一个通过查询字符串的Web应用程序。它工作得很好在IE和火狐,但被抛出异常每当我尝试在谷歌Chrome浏览器。该方案是窒息下面一行:

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)

我的结论是,转换只是不胜任工作,并着手试图让DateTime.ParseExact驯服这头野兽。但是,到目前为止,正确的格式字符串已躲避我。这里的code,我一直在努力(这不工作):

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

本页面被称为从另一个页面,通过一些JavaScript被称为由第三方组件(DayPilotCalendar)。下面是设置在DayPilotCalendar控制的相关属性:

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?

推荐答案

也许我建议,而不是通过这样的:在您的查询字符串08:00:00 GMT-0400(东部夏令时间)周三2012年10月3日 ,那不是你简单地传递日期的时间戳?例如,新的Date()。的getTime()。 (毫秒数自1970年以来,UTC)。然后,在C#中,你可以只是做:

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.

这篇关于DateTime.ParseExact格式字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆