DateTime and ParseExact not working properly
I am using AJAX and sending certain data:
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);
}
});
In the code behind:
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime bd = DateTime.ParseExact(BeginDate,"MM/DD/YYYY", provider);
DateTi开发者_StackOverflow中文版me ed = DateTime.ParseExact(EndDate, "MM/DD/YYYY", provider);
However when I run it i get an error saying
"String was not recognized as a valid Datetime"
Can u please help me in getting rid of this.
Or is there any other way to convert string to a DateTime?
In .NET, a correct custom date/time format string for a date string like "01/01/2011" is "MM/dd/yyyy"
Try
DateTime bd = DateTime.ParseExact(BeginDate,"MM/dd/yyyy", provider);
DateTime ed = DateTime.ParseExact(EndDate, "MM/dd/yyyy", provider);
or
DateTime bd = DateTime.ParseExact(BeginDate,"M/d/yyyy", provider);
DateTime ed = DateTime.ParseExact(EndDate, "M/d/yyyy", provider);
精彩评论