使用asp.net我查看了一个cookie值并查看
FireFox,Chrome:
"Thu Nov 25 2010 16:42:26 GMT-0500 (Eastern Standard Time)"
IE8:
"Thu Nov 25 16:48:01 EST 2010"
我像这样在JS中设置它。
$.cookie('pluginLastDate', new Date);
DateTime.Parse会引发这两种日期样式的异常。我如何获得asp.net兼容日期?
答案 0 :(得分:1)
一种可能的解决方案是在Javascript中创建的Date对象上使用toDateString()
方法来获取可以在C#中解析的日期字符串。
$.cookie('pluginLastDate', new Date().toDateString());
这将使Javascript日期为Thu Nov 25 2010
。但是,如果您需要更多控制确切的日期输出,您始终可以使用Date对象提供的许多方法手动在Javascript中构造日期字符串。以下链接提供了对Javascript Date的非常好的介绍。
http://blog.boyet.com/blog/javascriptlessons/javascript-for-c-developers-date-basics/
答案 1 :(得分:0)
我最后写了一些代码。
在JS中写这个
$.cookie('pluginLastDate', (new Date).toUTCString());
然后使用System.Text.RegularExpressions来解析日期时间
string[] Months = new string[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
DateTime GetJSDate(string sz)
{
var m = Regex.Match(sz, @", (\d+) (\w+) (\d{4,}) (\d{2})\:(\d{2})\:(\d{2}) (GMT|UTC)");
if (m.Success == false)
throw new Exception("Fail :(");
var y = Int32.Parse(m.Groups[3].Value);
var mo = Array.IndexOf(Months, m.Groups[2].Value) + 1;
var day = Int32.Parse(m.Groups[1].Value);
var h = Int32.Parse(m.Groups[4].Value);
var min = Int32.Parse(m.Groups[5].Value);
var se = Int32.Parse(m.Groups[6].Value);
var d = new DateTime(y, mo, day, h, min, se, DateTimeKind.Utc);
return d;
}