C# has it's own DateTime
structure. The goal is to
- convert your string to a
DateTime
and then
- convert your
DateTime
back to a string.
First, you need to get the format string for both formats X and Y. Check the following two lists:
E.g., for 12/31/2015
, all of d
or MM/dd/yyyy
(with the en-US or invariant culture) or MM/dd/yyyy
(with any locale) would be fine. For 2015-12-31 00:00:00
, it would be yyyy-MM-dd HH:mm:ss
.
For the first step, you can use DateTime.ParseExact (or DateTime.TryParseExact, if you want to fail gracefully if the string does not have the correct format), e.g.,
var myDateTime = DateTime.ParseExact(myInputString, "MM/dd/yyyy",
CultureInfo.InvariantCulture);
For the second step, use DateTime.ToString:
var myOutputString = myDateTime.ToString("yyyy-MM-dd HH:mm:ss",
CultureInfo.InvariantCulture);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…