DateTime分析不起作用
本文关键字:不起作用 DateTime | 更新日期: 2023-09-27 18:22:24
解析string
到DateTime
时抛出异常:
字符串未被识别为有效的DateTime
我的代码如下:
public ActionResult Create(string Sprint_Title, int Length, string Project, string Start_Date)
{
// Method 1
DateTime date = new DateTime();
date = DateTime.Parse(Start_Date.Trim().ToString()).Date;
//Method 2
DateTime? date = new DateTime();
date = DateTime.Parse(Start_Date).Date;
}
我该怎么做?
编辑:
js代码为:
$(".datepicker").datepicker({ dateFormat: "dd-mm-yy" });
$("#submit-button").click(function () {
$("#createForm").dialog("close");
var PL = $("#Project").val();
var SprintTitle = $("#SprintTitle").val();
var Length = $("#Length").val();
var StartDate = $("#StartDate").val();
var json = JSON.stringify({ Sprint_Title: SprintTitle, Length: Length, Project: PL, Start_Date: StartDate });
$.ajax({
url: "/Sprint/Create",
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: json,
dataType: 'json',
success: function (data) {
console.log(data);
window.location.reload(true);
}
});
});
使用TryParseExact
而不是Parse
,并提供您的确切格式。
CultureInfo enUS = new CultureInfo("en-US");
string dateString;
DateTime dateValue;
// Parse date with no style flags.
dateString = " 5/01/2009 8:30 AM";
if (DateTime.TryParseExact(dateString, "g", enUS,
DateTimeStyles.None, out dateValue))
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
// Allow a leading space in the date string.
if (DateTime.TryParseExact(dateString, "g", enUS,
DateTimeStyles.AllowLeadingWhite, out dateValue))
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
// Use custom formats with M and MM.
dateString = "5/01/2009 09:00";
if (DateTime.TryParseExact(dateString, "M/dd/yyyy hh:mm", enUS,
DateTimeStyles.None, out dateValue))
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
// Allow a leading space in the date string.
if (DateTime.TryParseExact(dateString, "MM/dd/yyyy hh:mm", enUS,
DateTimeStyles.None, out dateValue))
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
// Parse a string with time zone information.
dateString = "05/01/2009 01:30:42 PM -05:00";
if (DateTime.TryParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS,
DateTimeStyles.None, out dateValue))
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
// Allow a leading space in the date string.
if (DateTime.TryParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS,
DateTimeStyles.AdjustToUniversal, out dateValue))
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
// Parse a string represengting UTC.
dateString = "2008-06-11T16:11:20.0904778Z";
if (DateTime.TryParseExact(dateString, "o", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dateValue))
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
if (DateTime.TryParseExact(dateString, "o", CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind, out dateValue))
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
// The example displays the following output:
// ' 5/01/2009 8:30 AM' is not in an acceptable format.
// Converted ' 5/01/2009 8:30 AM' to 5/1/2009 8:30:00 AM (Unspecified).
// Converted '5/01/2009 09:00' to 5/1/2009 9:00:00 AM (Unspecified).
// '5/01/2009 09:00' is not in an acceptable format.
// Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 11:30:42 AM (Local).
// Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 6:30:42 PM (Utc).
// Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 9:11:20 AM (Local).
// Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 4:11:20 PM (Utc).
MSDN。
Error表示字符串输入的格式错误。它不是date time
形式。
如果它在您的过程中正常,只需使用DateTime.TryParse
方法检查输入值即可。
如果这是一个意外的情况,那么只需检查方法的调用方并修复它以发送正确的datetime值。
正如我所看到的,你的方法是Controller Action
方法。因此,只需将参数类型string
更改为date time
即可。
我会发表评论,但我没有足够的代表。
运行您的代码并用断点停止程序,这样您就可以读取字符串Start_Date。
以下示例的字符串Start_Date="19_5_1997 23:59:59:9999"。
DateTime FormatDateTime(string Start_Date)
{
DateTime startDate = new DateTime();
Start_Date = Start_Date.Trim();
char dateSeperator = '_';
char timeSeperator = ':';
char dateTimeSeperator = ' ';
string temp = "";
int dateCounter = 0;
int timeCounter = 0;
for (int i = 0; i < Start_Date.Length; i++)
{
if (dateSeperator == Start_Date[i])
{
if (0 == dateCounter)
{
startDate.AddDays(System.Convert.ToInt32(temp) - startDate.Day);
}
else if (1 == dateCounter)
{
startDate.AddMonths(System.Convert.ToInt32(temp) - startDate.Month);
}
dateCounter++;
temp = "";
}
else
if (dateTimeSeperator == Start_Date[i])
{
startDate.AddYears(System.Convert.ToInt32(temp) - startDate.Year);
temp = "";
}
else
if (timeSeperator == Start_Date[i])
{
if (0 == timeCounter)
{
startDate.AddHours(System.Convert.ToInt32(temp) - startDate.Hour);
}
else if (1 == timeCounter)
{
startDate.AddMinutes(System.Convert.ToInt32(temp) - startDate.Minute);
}
else if (2 == timeCounter)
{
startDate.AddSeconds(System.Convert.ToInt32(temp) - startDate.Second);
}
timeCounter++;
temp = "";
}
else
{
temp += Start_Date[i];
if (Start_Date.Length == i + 1)
{
startDate.AddMilliseconds(System.Convert.ToInt32(temp) - startDate.Millisecond);
}
}
}
return startDate;
}
这不是一个有效的方法,所以先检查一下你是否在任何地方都没有犯愚蠢的错误。