How would you handle the following string value that needs to be converted to a DateTime object?
"2015/01/22 12:08:51 (GMT+09:00)"
Would like to include this as a recognized DateTime pattern. As I encounter other formats, I would like to just implement a new pattern.
Using
DateTime.ParseExact
is probably your best bet. It takes in an input string and an expected format string that the input should match. It will returntrue
if the conversion was successful, and theout
parameter will be the result of the conversion (result
in the example below).I was unable to get it to work without forcibly removing the "GMT" portion, but if that's acceptable to you, the code below should work.
This example takes the original input and converts it to UTC time (i.e. it adjusts the time based on your
GMT
value, which is to subtract 9 hours in your example):This example is modified from the ones on the DateTime.TryParseExact documentation.