3

the code:

DateTime.Now.AddHours(-3).AddMinutes(-33).AddMonths(3).AddDays(21).ToString("ddMMMy Hmm")

gives the result:

 28May19 901

Hovever when I pass this date into the TryParseExact method I get the following :

DateTime.TryParseExact("28May19 901", "ddMMMyy Hmm", null, System.Globalization.DateTimeStyles.None, out dt);
false

How could this be false? and what would the correct format be?

EDIT:

CultureInfo.InvariantCulture does not make a difference:

DateTime.TryParseExact("28May19 901", "ddMMMyy Hmm", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt); false
Mark
  • 53
  • 6
  • Depends on your current culture. For me it would also be false because my May is Mai. Use `CultureInfo.InvariantCulture` instead of `null` (**Edit** but it's still false for me) – Tim Schmelter Feb 07 '19 at 12:46
  • @Rango still false and not working – Mark Feb 07 '19 at 12:49
  • `DateTime.TryParseExact("28May19 901", "ddMMMyy Hmm", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt); false` – Mark Feb 07 '19 at 12:50
  • DateTime.TryParseExact("28May19 9:01", "ddMMMyy H:mm", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt); true – Gowtham Alan Feb 07 '19 at 12:54
  • @Gowtham Alan the string is being parsed in without the colon separator – Mark Feb 07 '19 at 12:57
  • @Gowtham Alan is there a way without using the colon seperator? – Mark Feb 07 '19 at 12:57
  • 5
    This is the correct duplicate: https://stackoverflow.com/questions/2016206/net-why-is-tryparseexact-failing-on-hmm-and-hmmss (someone close please). Without a leading zero your hours are read as `90` which is of course no correct hour. – Tim Schmelter Feb 07 '19 at 12:58
  • as @Rango said, this is possible duplicate of https://stackoverflow.com/questions/2016206/net-why-is-tryparseexact-failing-on-hmm-and-hmmss – Marijke Buurlage Feb 07 '19 at 13:05
  • should I delete the question? – Mark Feb 07 '19 at 13:18

0 Answers0