I have this conversion:
DateTime dateTime;
DateTime.TryParseExact("01/02/2013", "mm/dd/yyyy", null, DateTimeStyles.None, out dateTime);
The following Assert fails:
Assert.AreEqual(new DateTime(2013, 1, 2), dateTime);
Because TryParseExact adds one hour to the datetime:
Expected: 2013-01-02 00:00:00.000
But was: 2013-01-02 00:01:00.000
Is this related to daylight savings time, and if so, does that mean I shouldn't be using DateTimeStyles.None?
Your format "mm/dd/yyyy" means minutes/days/years. You want "MM/dd/yyyy" (uppercase for month). Also, by passing null you actually say: use the current culture's datetime format. You probably want to use CultureInfo.InvariantCulture.
DateTime.TryParseExact("01/02/2013", "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);
This also prevents issues if the current culture does not use / as date-separator. Germany, for example, uses . as separator, hence the format-string "MM/dd/yyyy" would be evaluated as "MM.dd.yyyy". Read.
There is an issue with your format, lower case mm is used for minutes for month it is upper case MM. The difference you see between the values is of 1 minutes, not one hour.
Use:
DateTime.TryParseExact("01/02/2013", "MM/dd/yyyy", null, DateTimeStyles.None, out dateTime);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With