C# 将UTC datetime转换为MVC4中的DateTimeoffset

C# 将UTC datetime转换为MVC4中的DateTimeoffset,c#,jquery,asp.net-mvc-4,C#,Jquery,Asp.net Mvc 4,我将日期作为字符串,格式如下: string s = "Thu Aug 22 00:00:00 UTC+0530 2013"; 我必须将其转换为DatetimeOffset,我使用了Parse,ExactParse方法。然而,我似乎无法正确理解它。我发现字符串格式不正确异常 我必须将上述值作为datetimeoff对象存储在数据库中 出厂输出:2013-08-22 00:00:00+05:30。 从jQuery呈现的日期: $.widget("df.datetime", $.df.dateti

我将日期作为字符串,格式如下:

string s = "Thu Aug 22 00:00:00 UTC+0530 2013";
我必须将其转换为
DatetimeOffset
,我使用了
Parse
ExactParse
方法。然而,我似乎无法正确理解它。我发现
字符串格式不正确
异常

我必须将上述值作为
datetimeoff
对象存储在数据库中

出厂输出:
2013-08-22 00:00:00+05:30。

从jQuery呈现的日期:

$.widget("df.datetime", $.df.datetimecontrols, {
    _createInput: function () {
        var min = this.element.attr("data-minRelDate"),
            max = this.element.attr("data-maxRelDate");
        debugger;
        this._Input = $("<input>")
            .addClass("datetime")
            .attr("disabled", this._getDisableProp() ? "disabled" : "")
            .prop("disabled", this._getDisableProp() ? true : false)
            .addClass(this._getDisableProp() ? "disabled" : "")
            .datetimepicker({
                numberOfMonths: 2,
                minDate: min,
                maxDate: max,
                //Uncomment below line for date format.
                //dateFormat: $.datepicker.RFC_1123,
                timeText: "Current time:",
                hourGrid: 2,
                minuteGrid: 5,
                timeFormat: "hh:mm TT",
                onSelect: $.proxy(this._change, this),
                beforeShow: $.proxy(this._focusHndlr, this, 4),
                onClose: $.proxy(this._focusHndlr, this, -4)
                //TimeZone is not supported Across the browsers.To do manually there will change in the 
                //  years(save light day etc.,) https://github.com/timrwood/moment/issues/162
            })
            .focus($.proxy(this._inputFocus, this))
            .blur($.proxy(this._inputBlur, this))
            .appendTo(this._Wrapper);

        //Base element value to be widgets value.
        if ($(this.element).val() != "") {
            // If we wont specify time on recreate then time sliders will stay unchanged.
            //  we manipulate datepicker value and value of input to display differently.
            //  LLLL--> Thursday, April 18 2013 1:20 PM
            //  L --> 04/18/2013
            //  LT --> 8:30 PM
           this._Input.datepicker("setDate", new    Date(moment($(this.element).val()).format("LLLL")));
           this._Input.val(moment($(this.element).val()).format("L LT"));
        }
    },
$.widget(“df.datetime”,$.df.datetimecontrols{
_createInput:函数(){
var min=this.element.attr(“数据minRelDate”),
max=this.element.attr(“数据maxRelDate”);
调试器;
此._输入=$(“”)
.addClass(“日期时间”)
.attr(“已禁用”,this.\u getDisableProp()?“已禁用”:“
.prop(“已禁用”,此._getDisableProp()?真:假)
.addClass(此._getDisableProp()?“已禁用”:“”)
.日期时间选择器({
月数:2,
minDate:min,
maxDate:max,
//取消日期格式行下方的注释。
//dateFormat:$.datepicker.RFC_1123,
timeText:“当前时间:”,
沙格里德:2,
分钟网格:5,
时间格式:“hh:mm TT”,
onSelect:$.proxy(此.\u更改,此),
在显示之前:$.proxy(这个._focusndler,这个,4),
onClose:$.proxy(this.\u focushdler,this,-4)
//跨浏览器不支持时区。若要手动执行,将在
//年(节省光日等)https://github.com/timrwood/moment/issues/162
})
.focus($.proxy(this.\u inputFocus,this))
.blur($.proxy(this.\u inputBlur,this))
.appendTo(本包装);
//基本元素值为小部件值。
if($(this.element).val()!=“”){
//如果我们不在重新创建时指定时间,那么时间滑块将保持不变。
//我们操纵datepicker值和输入值以进行不同的显示。
//LLLL-->2013年4月18日星期四下午1:20
//L-->2013年4月18日
//LT-->晚上8:30
this.u Input.datepicker(“setDate”),新日期(moment($(this.element).val()).format(“LLLL”);
this._Input.val(矩($(this.element).val()).format(“L LT”);
}
},
试试这个:

DateTimeOffset.ParseExact("Thu Aug 22 00:00:00 UTC+0530 2013",
                          "ddd MMM dd HH:mm:ss \"UTC\"zzz yyyy",
                          CultureInfo.InvariantCulture);
编辑: 处理格式的基本方法。您可以使用正则表达式,或者如果您可以提前确定格式,则可以使用类似“type”的标志

或者,您可以使用自己的签名覆盖DateTimeOffset.ParseExact方法


最好为每种格式创建您自己的类类型,这些类型可以用您的通用解析方法的不同实现实现相同的接口。并且您的代码可以在运行时决定应该使用哪种实现(依赖项注入).

在此定义正确的内容。换句话说,请告诉我们所需的输出以及您得到的结果。格式化您的问题。如果上下文丢失,请随时恢复更改。这很好。但有时我会将日期时间字符串设置为“Thu,Jun 13 00:00 GMT”我怎么才能把它转换成DATETMEOSPLOY,我不知道这里的偏移量?@ MMSSAAN如何规范你的数据?选择你想要的格式,在字符串中间或结尾的一年。在这种情况下,你必须编写你的通用解析器,在那里你处理所有的格式。我没有任何特定的地方来处理格式。请参阅T。o我的其他问题:。如果我更改屏幕中的值,我得到的值为“Thu Aug 22 00:00:00 UTC+0530 2013”。如果我不更改并单击提交,我得到的值为“Thu,Jun 13 00:00:00 GMT”。顺便说一下,我们在jquery中呈现datetime picker中的日期,我将更新问题中的jquery脚本。jquery脚本已添加到问题中
 public DateTimeOffset universalParser(string inputDate, int type)
    {
        switch (type)
        {
            case 1:
                return DateTimeOffset.Parse(inputDate,
                                            CultureInfo.InvariantCulture);

            case 2:
                return DateTimeOffset.ParseExact(inputDate,
                                  "ddd MMM dd HH:mm:ss \"UTC\"zzz yyyy",
                                   CultureInfo.InvariantCulture);                         
        }
        //if there is another type
        return DateTimeOffset.Parse(inputDate);
    }