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.ParseExactis probably your best bet. It takes in an input string and an expected format string that the input should match. It will returntrueif the conversion was successful, and theoutparameter will be the result of the conversion (resultin 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
GMTvalue, which is to subtract 9 hours in your example):This example is modified from the ones on the DateTime.TryParseExact documentation.