0

I have a date time string that looks like this:

13.08.2014 17:17:45.000 UTC-60

I am trying to parse it into a C# date time object but it is not working as I expected.

Here is what I tried:

DateTime.ParseExact(dateToParse, "dd.MM.yyyy hh:mm:ss.fff Z", CultureInfo.InvariantCulture);

DateTime.ParseExact(dateToParse, "dd.MM.yyyy hh:mm:ss.fff UTC", CultureInfo.InvariantCulture);

DateTime.ParseExact(checkInDate, "dd.MM.yyyy hh:mm:ss.fff", CultureInfo.InvariantCulture);

They all return same error

{"String was not recognized as a valid DateTime."}

Some of the existing questions like this did not help either.

Any suggestions?

17
  • Why didn't the question you linked to help? What was wrong/missing with the existing answer? Commented Feb 4, 2018 at 10:29
  • 1
    Also, what does the-60 after the UTC represent? Commented Feb 4, 2018 at 10:30
  • -60 is not a valid UTC Offset value if you consider it as such. Commented Feb 4, 2018 at 10:31
  • As you can see in the accepted answer, adding Z in the end helped the person who asked the question, also other answers that suggested adding UTC did not help for my case. As you can see I have tried parsing as per the answers provided there but I get same error "string was not recognized as a valid date time" Commented Feb 4, 2018 at 10:34
  • 1
    @Cybercop - That was part of my point about being careful with the format strings. Commented Feb 4, 2018 at 10:57

2 Answers 2

3

First, your main problem there with parsing is that your're using hh for 24h format. That should be HH. This should work:

DateTime.ParseExact("13.08.2014 17:17:45.000", "dd.MM.yyyy HH:mm:ss.fff", null, System.Globalization.DateTimeStyles.AssumeUniversal)

As for the UTC part, that's not standard format, so I suggest you to create a helper method that splits this string in 2, parse the first part as provided above, and parse the number after UTC and either add that to your DateTime:

myDate.AddMinutes(Int32.Parse("-60"))

Or create a DateTimeOffset. In either case, you must parse them individually.

Sign up to request clarification or add additional context in comments.

2 Comments

This won't work either, it will throw an error due to the UTC-60 suffix.
@DavidG the code is working fine, if you read what I said. There shouldn't be UTC there to parse.
0

How much control do you have over the time format. .Net datetime parsing expects 2 things that are wrong with the current time format that you are trying to parse:

First, you have 24 hour time, so in your format you must use HH for hours, the lower case hh implies that the hours will be 12 hour format.

The UTC issue is another one that will require you to modify the string first, .Net expects timezone information in the form of HH:mm, so the following string and conversion will work, notice the key differences

var dateToParse = "13.08.2014 17:17:45.000 -01:00";
var value = DateTimeOffset.ParseExact(dateToParse, "dd.MM.yyyy HH:mm:ss.fff zzz", CultureInfo.InvariantCulture);
  1. Use DateTimeOffset to maintain the TimeZone information
  2. HH to map the hours
  3. zzz to map the timezone information

So, to address you question, how can we parse the string into a format that we can then use to parse into a date time:

dateToParse = "13.08.2014 17:17:45.000 UTC-60";
string utc = null;
if (dateToParse.Contains("UTC"))
{
    var tokens = dateToParse.Split(new string[] { "UTC" }, StringSplitOptions.None);
    dateToParse = tokens[0];
    utc = tokens[1];

    int minutes = int.Parse(utc);
    var offset = TimeSpan.FromMinutes(minutes);
    bool negative = offset.Hours < 0;
    dateToParse += (negative ? "-" : "") + Math.Abs(offset.Hours).ToString().PadLeft(2,'0') + ":" + offset.Minutes.ToString().PadLeft(2,'0');
}
var value = DateTimeOffset.ParseExact(dateToParse, "dd.MM.yyyy HH:mm:ss.fff zzz", CultureInfo.InvariantCulture);

To be honest, that was more complicated than I thought, there might be some regex expressions that might help, but this first principals approach to manipulating the string first works with your string.

Finally, now that we have a DateTimeOffset value, you can easily convert this into any local or other timezone without too much hassel, if you need to:

var asUtc = dateValue.UtcDateTime;
var asLocal = dateValue.LocalDateTime;
var asSpecific = dateValue.ToOffset(TimeSpan.FromHours(10)).DateTime;

4 Comments

I like your answer but bool negative = offset.Hours < 0; is not correct we cannot compare timespan with int. Just letting you know
Probably offset < TimeSpan.FromMinutes(0); will be better
@Cybercop offset.Hours is an int, that code absolutely compiles :)
My bad I just had offset and not offset.Hours

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.