简体   繁体   中英

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?

As far as I saw, there is no mention of any format for the string parameter that is being passed on to this method.

No mention?

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.

First of all, it is Convert.ToDateTime("08/01/2014") not 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. 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;

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. The value argument must contain the representation of a date and time in one of the formats described in the DateTimeFormatInfo topic.

So, what is DateTimeFormatInfo topic here exactly? It is the DateTimeFormatInfo 's information with DateTimeFormatInfo.GetAllDateTimePatterns method .

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 .

You can see all standard date and time formats of your CurrentCulture with;

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 ). And it has dd.MM.yyyy as a standard date and time format.

That's why when I write Convert.ToDateTime("02.01.2014") in my code, it parses this string as January 2nd not February 1st .

@SonerGönül My CurrentCulture is "en-US"! – Salihdeen 3 mins ago

Exactly as I said, your en-US has MM/dd/yyyy but not dd/MM/yyyy as a standard date and time format. That's why your 08/01/2014 string parsed with MM/dd/yyyy pattern not 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/y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
hh:mm:ss tt
H:mm:ss
HH:mm:ss
yyyy'-'MM'-'dd HH':'mm':'ss'Z'
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
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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM