For example, when the date in regional options is set:
Date Format: yyyy-mm-dd
Time settings:
Time Format: TT hh: mm: SS
Am Symbol: Morning
PM Symbol: PM
Console. writeline (datetime. Now. tostring ());
Output result: 02:11:56
Obviously, if you directly store this value in the database, a similar exception will be obtained: syntax error converting datetime from character string.
If you directly convert this string, such as datetime. prase ("02:11:56 "),CodeIf the date and time format of the current region is changed, the string was not recognized as a valid datetime.
Solution: Use the datetime. tostring (string format) method to convert the value of this instance to its equivalent string representation using the specified format.
Code
Datetime dt=Datetime. parse (datetime. Now. tostring ("Yyyy-mm-dd hh: mm: SS"));
In this case, the datetime output format is controlled by the format parameter and is irrelevant to the date/time setting in regional options.