c# - Format of Convert.ToDateTime(string) method -
when execute convert.todatetime('08/01/2014')
how convert 1-aug-2014 (ignore format) , not 8-jan-2014?
as far saw, there no mention of format string parameter being passed on method.
first of all, convert.todatetime("08/01/2014")
not convert.todatetime('08/01/2014')
. strings represents double quotes, not single quotes.
convert.todatetime(string)
method uses currentculture
iformatprovider
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 not null, return value result of invoking datetime.parse method on value using formatting information in datetimeformatinfo object initialized current culture. the value argument must contain representation of date , time in 1 of formats described in datetimeformatinfo topic.
so, datetimeformatinfo
topic here exactly? datetimeformatinfo
's information datetimeformatinfo.getalldatetimepatterns
method.
most current thread culture has mm/dd/yyyy
not dd/mm/yyyy
(or character dateseparator
of currentculture
because "/"
custom format specifier has special meaning replace me current culture or specified culture date separator) standard date , time format.
you can see standard date , time formats of currentculture
with;
var patterns = cultureinfo.currentculture. datetimeformat. getalldatetimepatterns(); foreach (var format in patterns) { console.writeline(format); }
let me give example; current thread culture turkish (tr-tr
). , has dd.mm.yyyy
standard date , time format.
that's why when write convert.todatetime("02.01.2014")
in code, parses string january 2nd not february 1st.
@sonergönül currentculture "en-us"! – salihdeen 3 mins ago
exactly said, en-us
has mm/dd/yyyy
not dd/mm/yyyy
standard date , time format. that's why 08/01/2014
string parsed 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 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 mm/dd/yyyy hh:mm tt mm/dd/yyyy h:mm mm/dd/yyyy hh:mm yy/mm/dd h:mm tt yy/mm/dd hh:mm tt yy/mm/dd h:mm yy/mm/dd hh:mm yyyy-mm-dd h:mm tt yyyy-mm-dd hh:mm tt yyyy-mm-dd h:mm yyyy-mm-dd hh:mm dd-mmm-yy h:mm tt dd-mmm-yy hh:mm tt dd-mmm-yy h:mm dd-mmm-yy hh:mm m/d/yyyy h:mm:ss tt m/d/yyyy hh:mm:ss tt m/d/yyyy h:mm:ss m/d/yyyy hh:mm:ss m/d/yy h:mm:ss tt m/d/yy hh:mm:ss tt m/d/yy h:mm:ss m/d/yy hh:mm:ss mm/dd/yy h:mm:ss tt mm/dd/yy hh:mm:ss tt mm/dd/yy h:mm:ss mm/dd/yy hh:mm:ss mm/dd/yyyy h:mm:ss tt mm/dd/yyyy hh:mm:ss tt mm/dd/yyyy h:mm:ss mm/dd/yyyy hh:mm:ss yy/mm/dd h:mm:ss tt yy/mm/dd hh:mm:ss tt yy/mm/dd h:mm:ss yy/mm/dd hh:mm:ss yyyy-mm-dd h:mm:ss tt yyyy-mm-dd hh:mm:ss tt yyyy-mm-dd h:mm:ss yyyy-mm-dd hh:mm:ss dd-mmm-yy h:mm:ss tt dd-mmm-yy hh:mm:ss tt dd-mmm-yy h:mm:ss dd-mmm-yy hh:mm:ss mmmm dd mmmm dd yyyy'-'mm'-'dd't'hh':'mm':'ss.fffffffk yyyy'-'mm'-'dd't'hh':'mm':'ss.fffffffk ddd, dd mmm yyyy hh':'mm':'ss 'gmt' ddd, dd mmm yyyy hh':'mm':'ss 'gmt' yyyy'-'mm'-'dd't'hh':'mm':'ss h:mm tt hh:mm tt h:mm hh:mm h:mm:ss tt 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
Comments
Post a Comment