[英]DateTime.Parse throw exception in C#
我不知道為什么拋出異常,這是工作代碼:
DateTime.Parse("1/12/2012 12:00:00 AM")
這是拋出異常的那個:
DateTime.Parse("1/13/2012 12:00:00 AM")
拋出的異常是“FormatException” ,包含此消息: “字符串未被識別為有效的DateTime”。
這是CurrentCulture值:
System.Globalization.CultureInfo.CurrentCulture
{en-MY}
Calendar: {System.Globalization.GregorianCalendar}
CompareInfo: {CompareInfo - en-MY}
CultureTypes: SpecificCultures | InstalledWin32Cultures
DateTimeFormat: {System.Globalization.DateTimeFormatInfo}
DisplayName: "English (Malaysia)"
EnglishName: "English (Malaysia)"
IetfLanguageTag: "en-MY"
IsNeutralCulture: false
IsReadOnly: true
KeyboardLayoutId: 17417
LCID: 17417
Name: "en-MY"
NativeName: "English (Malaysia)"
NumberFormat: {System.Globalization.NumberFormatInfo}
OptionalCalendars: {System.Globalization.Calendar[2]}
Parent: {en}
TextInfo: {TextInfo - en-MY}
ThreeLetterISOLanguageName: "eng"
ThreeLetterWindowsLanguageName: "ENM"
TwoLetterISOLanguageName: "en"
UseUserOverride: true
有誰知道這里發生了什么?
因為一年只有12個月;)
我猜你當前的文化背景使用'dd / MM / yyyy'。 使用Parse的重載指定要用於解析字符串的文化的日期格式:
DateTime.Parse(String, IFormatProvider)
或者使用ParseExact()方法並自己指定格式。
var provider = CultureInfo.InvariantCulture;
var format = "M/dd/yyyy hh:mm:ss tt";
DateTime.ParseExact("1/13/2012 12:00:00 AM", format, provider);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.