3

I'm having huge problems with solving this problem. I'm trying to parse a string using Datetime.ParseExact().

I have the following code:

DateTime.ParseExact("20151210 832", "yyyyMMdd Hmm", CultureInfo.InvariantCulture);

I get following error:

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: String was not recognized as a valid DateTime.

What am I doing wrong? How can I solve this problem?

UPDATE:

I can also get times like this:

00:01 => 1
01:00 => 1
01:10 => 10
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
mrjasmin
  • 1,230
  • 6
  • 21
  • 37

2 Answers2

5

Since H specifier can be 2 digit, this method try to parse your 83 with H specifier. Since there is no such an hour, you get FormatException.

For your case, one way to prevent this is putting a leading zero just before your 8.

var s = "20151210 832";
var result = s.Split(' ')[0] + " 0" + s.Split(' ')[1];
var dt = DateTime.ParseExact(result, "yyyyMMdd Hmm", CultureInfo.InvariantCulture);

Be aware, this will not work for all cases. For example, if your hour part already two digit, if your single minute does not have leading zero.. etc.

Or you can put delimiter for your all parts but in such a case, you need to manipulate both your string and format.

.NET Team suggest this way as well.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • @mrjasmin There is no format to parse your string _directly_. You need to manipulate your string. – Soner Gönül Dec 10 '15 at 09:05
  • @dotctor There are too many cases. If the hour part two digit, if the single minute part have or not leading zero.. You can't solve all of these with a _just_ one format. That's what I try to say. – Soner Gönül Dec 10 '15 at 09:11
  • Good explanation of the problem, but I have some comments: `mm` means that the minute part always has two digits so this comment `your single minutes have or nor leading zero` is irrelevant. `you need to manipulate both your string and format` - the format is specified only once for all cases and the string manipulation is necessary any way. – Sergii Zhevzhyk Dec 10 '15 at 09:22
  • @SergiiZhevzhyk You are correct. I changed _that_ sentence. What I try to say (I feel I couldn't explain myself clearly), this is a bad formatted `string` to parse `DateTime` (sorry OP but that's true). We can _not_ single minutes have leading zero or not for example. If input like `23`, what should we think? It is for only hours part as `23:00`? Is it for only minutes part as `00:23`? Or is it `2 hour and 3 minute` as `02:03`? That's impossible to know. That's why in my humble opinion, we _can_ generate only _specific_ solutions for _specific_ cases. – Soner Gönül Dec 10 '15 at 09:37
0

Just insert a separator before minutes (for example, a whitespace) and then you can parse it like this:

string example = "20151210 832";
example = example.Insert(example.Length - 2, " ");
var dateTime = DateTime.ParseExact(example, "yyyyMMdd H mm", CultureInfo.InvariantCulture);

I assume that the datetime string always contains two digits specifying minutes (check an article about Custom Date and Time Format Strings). If my assumption is wrong then this string cannot be parsed.

Sergii Zhevzhyk
  • 4,074
  • 22
  • 28
  • As we discussed, _even_ adding whitespace does not solve _single digit without leading zero_ problem. For example if time part is `##83` (I used `#` instead of white space because if brokes `` highlight), this will add white space before `8` not after and it will be like ` 83`. I blame _the_ string for that, not us :) – Soner Gönül Dec 10 '15 at 09:45