c# - Convert.ToDateTime() error -
i error trying convert string datetime, though has worked before.
this procedure used:
save datetime text file this:
datetime.now.touniversaltime().tostring(); //results in 20.9.2015 10.16.12on application load up:
string s = streamreader.readline(); //the saved string s = "20.09.2015 10.16.12" datetime d = convert.todatetime(s);
this results in this:
string not recognized valid datetime.
i have never experienced problem before installed windows 10 , visual studio 2015, previous setup windows 7 , visual studio 2013. weird thing results in same error:
datetime d = convert.todatetime(datetime.now.touniversaltime().tostring()); this did work in previous setup, ideas why not work more?
edit: believe question not duplicate of question converting string datetime thomas weller linked to. because problem result of changes in expected behaviour, see second example. did find fix this, not practical:
string s = datetime.now.touniversaltime().tostring(); s = s.substring(0, s.indexof(" ")).replace('.', '/') + s.substring(s.indexof(" ")).replace('.', ':'); datetime d = convert.todatetime(s);
this not work anymore due regional settings on control panel.
to avoid conflicts regional settings on target enviroment, use datetime.tryparseexact:
string s = streamreader.readline(); //the saved string s = "20.09.2015 10.16.12" datetime d = datetime.now; datetime.tryparseexact(s, "dd.mm.yyyy hh.mm.ss", cultureinfo.invariantculture, datetimestyles.none, out d); also, if default format , need format entire application, you can set default culture on config file.
this code:
convert.todatetime(datetime.now.touniversaltime().tostring()) should work on enviroment, once datetime.tostring() , convert.todatetime() without format provider uses same datetimeformatinfo, unless changing culture between these calls. note datetime.tostring() without format specifier will use general date/time pattern (g), based on current culture. , convert.datetime without formatprovider will use current culture too (check these references on msdn).
my last suggestion is, instead of doing replaces, can do:
string s = datetime.now.touniversaltime().tostring("dd/mm/yyyy hh:mm:ss");
C - Convert.Todatetime() Error - >>>>> Download Now
ReplyDelete>>>>> Download Full
C - Convert.Todatetime() Error - >>>>> Download LINK
>>>>> Download Now
C - Convert.Todatetime() Error - >>>>> Download Full
>>>>> Download LINK 38