[英]Format of Convert.ToDateTime(string) method
When we execute Convert.ToDateTime('08/01/2014')
how does it convert to 1-Aug-2014 (ignore the format) and not to 8-Jan-2014? 当我们执行Convert.ToDateTime('08/01/2014')
如何将其转换为2014年8月1日(忽略格式)而不是2014年1月8日?
As far as I saw, there is no mention of any format for the string parameter that is being passed on to this method. 据我所知,没有提及传递给此方法的字符串参数的任何格式。
If value is not null , the return value is the result of invoking the DateTime.Parse method on value using the formatting information in a DateTimeFormatInfo object that is initialized for the current culture. 如果value不为null ,则返回值是使用针对当前区域性初始化的DateTimeFormatInfo对象中的格式信息对value调用DateTime.Parse方法的结果。
First of all, it is Convert.ToDateTime("08/01/2014")
not Convert.ToDateTime('08/01/2014')
. 首先,它是Convert.ToDateTime("08/01/2014")
而不是Convert.ToDateTime('08/01/2014')
。 Strings are represents with double quotes, not single quotes. 字符串用双引号而不是单引号表示。
Convert.ToDateTime(string)
method uses CurrentCulture
as an IFormatProvider
by default. 默认情况下, Convert.ToDateTime(string)
方法使用CurrentCulture
作为IFormatProvider
。 Here how it's implemented ; 这里是如何实现的 ;
public static DateTime ToDateTime(String value)
{
if (value == null)
return new DateTime(0);
return DateTime.Parse(value, CultureInfo.CurrentCulture);
}
And DateTime.Parse(string, IFormatProvider)
implemented as; 和DateTime.Parse(string, IFormatProvider)
实现为;
public static DateTime Parse(String s, IFormatProvider provider)
{
return (DateTimeParse.Parse(s, DateTimeFormatInfo.GetInstance(provider), DateTimeStyles.None));
}
From documentation ; 从文件 ;
If value is not null, the return value is the result of invoking the DateTime.Parse method on value using the formatting information in a DateTimeFormatInfo object that is initialized for the current culture. 如果value不为null,则返回值是使用针对当前区域性初始化的DateTimeFormatInfo对象中的格式信息对value调用DateTime.Parse方法的结果。 The value argument must contain the representation of a date and time in one of the formats described in the DateTimeFormatInfo topic. value参数必须以DateTimeFormatInfo主题中描述的格式之一包含日期和时间的表示形式。
So, what is DateTimeFormatInfo
topic here exactly? 那么,这里的DateTimeFormatInfo
主题到底是什么? It is the DateTimeFormatInfo
's information with DateTimeFormatInfo.GetAllDateTimePatterns
method . 它是带有DateTimeFormatInfo.GetAllDateTimePatterns
方法的DateTimeFormatInfo
的信息。
Most probably your current thread culture has a MM/dd/yyyy
not dd/MM/yyyy
(or which character is your DateSeparator
of your CurrentCulture
because "/"
custom format specifier has a special meaning as replace me current culture or specified culture date separator ) as a standard date and time format . 您当前的线程区域性很可能是MM/dd/yyyy
而不是dd/MM/yyyy
(或者哪个字符是CurrentCulture
的DateSeparator
,因为"/"
自定义格式说明符具有特殊的含义,因为它替换了我当前的区域性或指定的区域性日期分隔符 )作为标准日期和时间格式 。
You can see all standard date and time formats of your CurrentCulture
with; 您可以通过以下方式查看CurrentCulture
所有标准日期和时间格式:
var patterns = CultureInfo.CurrentCulture.
DateTimeFormat.
GetAllDateTimePatterns();
foreach (var format in patterns)
{
Console.WriteLine(format);
}
Let me give you an example; 让我给你举个例子; my current thread culture is Turkish ( tr-TR
). 我当前的线程文化是土耳其语( tr-TR
)。 And it has dd.MM.yyyy
as a standard date and time format. 它具有dd.MM.yyyy
作为标准日期和时间格式。
That's why when I write Convert.ToDateTime("02.01.2014")
in my code, it parses this string as January 2nd not February 1st . 这就是为什么当我在代码中编写Convert.ToDateTime("02.01.2014")
时,它将此字符串解析为1月2 日而不是2月1日的原因 。
@SonerGönül My CurrentCulture is "en-US"! @SonerGönül我的CurrentCulture是“ zh-cn”! – Salihdeen 3 mins ago – Salihdeen 3分钟前
Exactly as I said, your en-US
has MM/dd/yyyy
but not dd/MM/yyyy
as a standard date and time format. 就像我说的那样,您en-US
将MM/dd/yyyy
dd/MM/yyyy
作为标准日期和时间格式,但没有dd/MM/yyyy
作为标准日期和时间格式。 That's why your 08/01/2014
string parsed with MM/dd/yyyy
pattern not dd/MM/yyyy
. 这就是为什么您的08/01/2014
字符串使用MM/dd/yyyy
模式而不是dd/MM/yyyy
解析的原因。
var patterns = CultureInfo.GetCultureInfo("en-US").
DateTimeFormat.
GetAllDateTimePatterns();
foreach (var format in patterns)
{
Console.WriteLine(format);
}
Result will be; 结果将是;
M/d/yyyy
M/d/yy
MM/dd/yy
MM/dd/yyyy <-- HERE!
yy/MM/dd
yyyy-MM-dd
dd-MMM-yy
dddd, MMMM dd, yyyy
MMMM dd, yyyy
dddd, dd MMMM, yyyy
dd MMMM, yyyy
dddd, MMMM dd, yyyy h:mm tt
dddd, MMMM dd, yyyy hh:mm tt
dddd, MMMM dd, yyyy H:mm
dddd, MMMM dd, yyyy HH:mm
MMMM dd, yyyy h:mm tt
MMMM dd, yyyy hh:mm tt
MMMM dd, yyyy H:mm
MMMM dd, yyyy HH:mm
dddd, dd MMMM, yyyy h:mm tt
dddd, dd MMMM, yyyy hh:mm tt
dddd, dd MMMM, yyyy H:mm
dddd, dd MMMM, yyyy HH:mm
dd MMMM, yyyy h:mm tt
dd MMMM, yyyy hh:mm tt
dd MMMM, yyyy H:mm
dd MMMM, yyyy HH:mm
dddd, MMMM dd, yyyy h:mm:ss tt
dddd, MMMM dd, yyyy hh:mm:ss tt
dddd, MMMM dd, yyyy H:mm:ss
dddd, MMMM dd, yyyy HH:mm:ss
MMMM dd, yyyy h:mm:ss tt
MMMM dd, yyyy hh:mm:ss tt
MMMM dd, yyyy H:mm:ss
MMMM dd, yyyy HH:mm:ss
dddd, dd MMMM, yyyy h:mm:ss tt
dddd, dd MMMM, yyyy hh:mm:ss tt
dddd, dd MMMM, yyyy H:mm:ss
dddd, dd MMMM, yyyy HH:mm:ss
dd MMMM, yyyy h:mm:ss tt
dd MMMM, yyyy hh:mm:ss tt
dd MMMM, yyyy H:mm:ss
dd MMMM, yyyy HH:mm:ss
M/d/yyyy h:mm tt
M/d/yyyy hh:mm tt
M/d/yyyy H:mm
M/d/yyyy HH:mm
M/d/yy h:mm tt
M/d/yy hh:mm tt
M/d/yy H:mm
M/d/yy HH:mm
MM/dd/yy h:mm tt
MM/dd/yy hh:mm tt
MM/dd/yy H:mm
MM/dd/yy HH:mm
MM/dd/yyyy h:mm tt
M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dddd, MMMM dd, yyyy hh:mm:ss tt
dddd, MMMM dd, yyyy H:mm:ss
dddd, MMMM dd, yyyy HH:mm:ss
MMMM dd, yyyy h:mm:ss tt
MMMM dd, yyyy hh:mm:ss tt
MMMM dd, yyyy H:mm:ss
MMMM dd, yyyy HH:mm:ss
dddd, dd MMMM, yyyy h:mm:ss tt
dddd, dd MMMM, yyyy hh:mm:ss tt
dddd, dd MMMM, yyyy H:mm:ss
dddd, dd MMMM, yyyy HH:mm:ss
dd MMMM, yyyy h:mm:ss tt
dd MMMM, yyyy hh:mm:ss tt
dd MMMM, yyyy H:mm:ss
dd MMMM, yyyy HH:mm:ss
MMMM, yyyy
MMMM, yyyy
Use this. 用这个。
DateTime.ParseExact(string datetime, string format, IFormatProvider provider)
eg: 例如:
DateTime.ParseExact("08-01-2014", "MM-dd-yyyy", new CultureInfo("en-US"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.