将JS日期解析为C#DateTime [英] Parse JS date to C# DateTime

查看:62
本文介绍了将JS日期解析为C#DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JavaScript日期对象,它以这种格式为我提供了一个日期字符串,"Tue Sep 04 2012B0100(格林尼治标准时间夏令时)"

I have javascript date object which gives me a date string in this format, "Tue Sep 04 2012B0100 (GMT Daylight Time)"

我试图按照此处所述解析ParseEaxcat,但这会引发无效的日期异常-有人指出我正确格式的方向

I am trying to parse with ParseEaxcat as mentioned here, but it throws an invalid date exception - anyone point me in the direction of the right format

                string date = "Tue Sep 04 2012B0100 (GMT Daylight Time)";
                dt = DateTime.ParseExact(date,"ddd MMM dd yyyyBzzzz",
                     CultureInfo.InvariantCulture);

我也很不高兴地看着这个: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

I've also looked at this with no joy: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

推荐答案

这有效.不过,您可能要在传递GMT的夏令时"部分之前将其剥离,以免发生行内分割.

This works. Though, you may want to strip the GMT Daylight Time portion out before passing it in just to avoid the in-line split.

string date = "Tue Sep 04 2012B0100 (GMT Daylight Time)";
var dt = DateTime.ParseExact(date.Split('(')[0].Replace("B","+").Trim(), "ddd MMM dd yyyyzzz", CultureInfo.InvariantCulture);

已编辑以说明偏移量.

这篇关于将JS日期解析为C#DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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