1

I'm trying to find the appropriate format string to parse (exact) the following types of dates:

  • 1-01-01T00:00:00+00:00 - 1 January of 0001
  • 2011-12-14T15:53:40+00:00 - 14 December of 2011

So the year length seems to be variable (1-4 characters).

The format sting I currently use to parse exact is:
c_DateTimeFormatString = "yyyy-MM-ddTHH':'mm':'sszzz"

Obviously this only matches the second string. The first one poped up today. Now we have to match that as well.

Is there a format string to achieve this?

UPDATE #1
I added the actual dates in clear text after the input date strings.

UPDATE #2
Parse exact has an overload that allows for multiple format strings to be passed in. This seems to be the right way.

So the first try was to use:

DateTime.ParseExact("1-01-01T00:00:00+00:00 ", new[] { "yyyy-MM-ddTHH':'mm':'sszzz", "yyy-MM-ddTHH':'mm':'sszzz", "yy-MM-ddTHH':'mm':'sszzz", "y-MM-ddTHH':'mm':'sszzz" }, CultureInfo.CreateSpecificCulture("en-US"), DateTimeStyles.AssumeLocal)

But sadly this does ont give the correct result, the first date string is parsed as:
01.01.2001
rather than
01.01.0001

So the question now is what is the correct parsing string to parse year one which is represented with only one digit?

6
  • 2
    What is the correct year for that format? Is it 2011, 2001, the year 1? Dec 14, 2011 at 16:39
  • 1
    You say the first one popped up today. Are you sure that's not simply an invalid piece of information in your source data? It simply looks to me like the first portion of the value has been cut off. If this is the first you've seen it, I would instead explore why you are now seeing it. Dec 14, 2011 at 16:42
  • @Anthony No its a valid response, checked with the data provider, its simply a very unusual response since people are usually expected to fill out this date, but they don't necessarily have to
    – ntziolis
    Dec 14, 2011 at 16:50
  • @John I have just updated the question to make clear what the actual dates are
    – ntziolis
    Dec 14, 2011 at 16:51
  • 1
    The actual date that you've specified for the first date (1 January of 0001) does not make sense. It looks like it should be either 1 January of 2005 or 5 January of 0001. I'm guessing it should be the former. Dec 14, 2011 at 17:13

6 Answers 6

2

Updated based on comment:

string y = "yyyy-MM-ddTHH':'mm':'sszzz";

string testDate = "1-01-05T00:00:00+00:00".PadLeft(25, '0');
Console.WriteLine(DateTime.ParseExact(testDate, y, CultureInfo.InvariantCulture));
testDate = "2011-12-14T15:53:40+00:00".PadLeft(25, '0');
Console.WriteLine(DateTime.ParseExact(testDate, y, CultureInfo.InvariantCulture));

The output is:

1/5/0001 00:00:00
12/14/2011 15:53:40
3
  • I'd rather not use string padding, but if i have your solution looks like the most elegant
    – ntziolis
    Dec 14, 2011 at 17:37
  • I do understand that, but it seems that one way or another your have to deal with input length (even if your overload the method). Padding with leading zeros is the shortest path.
    – user1088520
    Dec 14, 2011 at 17:44
  • 1
    Just tried to implement it and with a minor fix I got it to work, if you update your answer I'll mark it as solved. The problem was that the format string has a different length than a "proper" date string that matches the date format string. So I just counted the the characters in a 'proper' date string so the final solution was: "1-01-05T00:00:00+00:00".PadLeft(25, '0')
    – ntziolis
    Dec 14, 2011 at 22:21
2

You can use an overload of ParseExact to match multiple formats, I believe.

See MSDN.

2
  • this seems to be the right way, but when using: DateTime.ParseExact("1-12-14T17:20:13+01:00", new[] { "yyyy-MM-ddTHH':'mm':'sszzz", "yyy-MM-ddTHH':'mm':'sszzz", "yy-MM-ddTHH':'mm':'sszzz", "y-MM-ddTHH':'mm':'sszzz" }, CultureInfo.CreateSpecificCulture("en-US"), DateTimeStyles.AssumeLocal)` the first string parses to 01.01.2001 rather than 01.01.0001 any ideas on the correct format string to match year one properly?
    – ntziolis
    Dec 14, 2011 at 16:53
  • I would have loved to use this way to solve this issue since I believe this is the right way of tackling an issue like this, sadly I could not come up with a format string that parses year one properly so I head to use string padding instead.
    – ntziolis
    Dec 15, 2011 at 9:03
0

If the input formats are not all exactly the same, you'll need either to stop using parse exact, or to call it with different format arguments depending on the format of the input data.

0

I don't believe you need to match a year from 1 to 4 characters but 2 or 4 characters.

For this example

1-01-05T00:00:00+00:00

you would need something like

d-MM-yyTHH':'mm':'sszzz

1
  • thx but the general format i provided is correct, the year is the first info in the string
    – ntziolis
    Dec 14, 2011 at 17:15
0

Try padding your string so that the year is 4 digits long. You should probably add 2, 20 or 201 and not just 0's.

1
  • would rather not do that, but prefer finding the correct parsing string
    – ntziolis
    Dec 14, 2011 at 17:35
0

Since you know that you have to support multiple formats, I suggest that you use the TryParseExact method.

If it fails to parse using one format (i.e. it returns false), then try the next format.

1
  • still need the correct format sting to parse year one, see the updated question
    – ntziolis
    Dec 14, 2011 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.