3

I’m trying to parse a time. I’ve seen this question asked/answered here many times but not for this specific scenario. Here’s my code:

var time1 = DateTime.ParseExact("919", "Hmm", CultureInfo.InvariantCulture);

also

var time2 = DateTime.ParseExact("919", "Hmm", null);

both of these throw the same

"String was not recognized as a valid DateTime"

What I want is 9:19 AM.

For further info I also need to parse “1305” as 1:05 PM, this is working fine.

It seems to me I’m using the correct format. What am I overlooking?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Alex
  • 1,681
  • 2
  • 11
  • 18

4 Answers4

10

I'm not sure there is any format that can handle this. The problem is that "H" can be either one digit or two, so if there are two digits available, it will grab both - in this case parsing it as hour 91, which is clearly invalid.

Ideally, you'd change the format to HHmm - zero-padding the value where appropriate - so "0919" would parse fine. Alternatively, use a colon in the format, to distinguish between the hours and the minutes. I don't believe there's any way of making DateTime parse a value of "919" as you want it to... so you'll need to adjust the string somehow before parsing it. (We don't have enough context to recommend a particular way of doing that.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Much appreciated for all the answers. I don’t have control of the text being created so the simplest solution for me seemed to be prefixing a zero as opposed to adding a colon in the middle. var text = "919"; var time = DateTime.ParseExact(text.PadLeft(4, '0'), "Hmm", null); – Alex Mar 10 '15 at 14:32
2

Yes, your format is right but since H specifier might be 2 character, ParseExact method try to parse 91 as an hour, which is an invalid hour, that's why you get FormatException in both case.

I connected to microsoft team about this situation 4 months ago. Take a look;

They suggest to use 2 digit form in your string or insert a date separator between them.

var time1 = DateTime.ParseExact("0919", "Hmm", CultureInfo.InvariantCulture);

or

var time1 = DateTime.ParseExact("9:19", "H:mm", CultureInfo.InvariantCulture);
Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

You cant exclude the 0 prefix to the hour. This works

var time1 = DateTime.ParseExact("0919", "Hmm", CultureInfo.InvariantCulture);

Perhaps you want to just prefix 3-character times with a leading zero before parsing.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • 3
    Well you *can*, but only if you've got a separator. The problem is that there are two digits that can be parsed... – Jon Skeet Mar 10 '15 at 14:21
0

Much appreciated for all the answers. I don’t have control of the text being created so the simplest solution for me seemed to be prefixing a zero as opposed to adding a colon in the middle.

var text = "919";

var time = DateTime.ParseExact(text.PadLeft(4, '0'), "Hmm", null);
Alex
  • 1,681
  • 2
  • 11
  • 18