DateTime和ParseExact无法正常工作

时间:2011-06-13 19:57:59

标签: c# asp.net jquery

我正在使用AJAX并发送某些数据:

  var eventtype = parseInt(record.<%= CEO.FieldEvaluator.GetEvaluatorByDId("EVENT_TYPE_ID").GetFieldDataFieldId()%>);
   var begindate = parseInt(record.<%= CEO.FieldEvaluator.GetEvaluatorByDId("BeginDate").GetFieldDataFieldId()%>);
   var enddate = parseInt(record.<%= CEO.FieldEvaluator.GetEvaluatorByDId("EndDate").GetFieldDataFieldId()%>);

        $.ajax({
            type: "POST", url: "Data.aspx/CheckInsertRecord",
            data: "{EventType:'" + eventtype + "',BeginDate:'" + begindate + "'," +
                   "EndDate:'" + enddate+"' }",
            contentType: "application/json; charset=utf-8", dataType: "json",
            success: function (msg) {
            alert(msg.d);

            }
        });

在背后的代码中:

  CultureInfo provider = CultureInfo.InvariantCulture;

    DateTime bd = DateTime.ParseExact(BeginDate,"MM/DD/YYYY", provider);
    DateTime ed = DateTime.ParseExact(EndDate, "MM/DD/YYYY", provider);

然而,当我运行它时,我得到一个错误说

  "String was not recognized as a valid Datetime"

请你帮我摆脱这个。

或者还有其他方法可以将字符串转换为DateTime吗?

2 个答案:

答案 0 :(得分:1)

在.NET中,对于像“01/01/2011”这样的日期字符串的正确custom date/time format string"MM/dd/yyyy"

答案 1 :(得分:0)

尝试

DateTime bd = DateTime.ParseExact(BeginDate,"MM/dd/yyyy", provider);
DateTime ed = DateTime.ParseExact(EndDate, "MM/dd/yyyy", provider);

DateTime bd = DateTime.ParseExact(BeginDate,"M/d/yyyy", provider);
DateTime ed = DateTime.ParseExact(EndDate, "M/d/yyyy", provider);