c# - Validation of DateTime Format -


i trying validate datetime format user can provide. here sample code ...

 datetime tempdatetime;  string _userformat = "aa";   string tempdatetime2 = datetime.now.tostring(_userformat);  bool b = datetime.tryparseexact(tempdatetime2, _userformat, cultureinfo.invariantculture, datetimestyles.none, out tempdatetime);   console.writeline("{0},{1}", tempdatetime, b);  console.readline(); 

this returns true (value of b) , valid datetime (tempdatetime). in impression return false because _userformat not valid format. there other way or missing something.

thank you

since calling tostring() format aa, not represent valid custom format code, code treated string literal , string value aa back.

when try parse "date" using same format specifier, sees format of source string matches format specifier, parses without error. since neither source nor format code specifies viable information data/time, "default" value of datetime.now.date used.

from msdn:

if format defines time no date element , parse operation succeeds, resulting datetime value has date of datetime.now.date.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -