-2

I have been trying to convert this string to a DateTime object in C#

2019-09-23T08:34:00UTC+1

I've tried using DateTime.Parse but it is throwing an exception for

"String was not recognized as a valid DateTime."

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Sean Davies
  • 31
  • 1
  • 11
  • 2
    Does this answer your question? [how to convert string to DateTime as UTC as simple as that](https://stackoverflow.com/questions/13254211/how-to-convert-string-to-datetime-as-utc-as-simple-as-that) – Ňɏssa Pøngjǣrdenlarp Nov 01 '19 at 14:04
  • Can you share the code ! – Chetan Nov 01 '19 at 14:06
  • No this throws the exception for "String was not recognized as a valid DateTime." – Sean Davies Nov 01 '19 at 14:09
  • 1
    It is unusual to see the UTC+1 part inside the string. Usually the ISO8601 format is used....where it comes from that string? If you remove UTC from the string is should work – ema Nov 01 '19 at 14:09

2 Answers2

2

I'm sorry but you seem like a victim of garbage in, garbage out.

That's an unusual format, that's why before I suggest a solution for you, first thing I want to say is "Fix your input first if you can".

Let's say you can't fix your input, then you need to consider a few things;

  1. First of all, if your string has some parts like UTC and/or GMT, there is no custom date and time format specifier to parse them. That's why you need to escape them as a string literal. See this question for more details.
  2. Second, your +1 part looks like a UTC Offset value. The "z" custom format specifier is what you need for parse it but be careful, this format specifier is not recommended for use with DateTime values since it doesn't reflect the value of an instance's Kind property.

As a solution for DateTime, you can parse it like I would suggest;

var s = "2019-09-23T08:34:00UTC+1";
DateTime dt;
if(DateTime.TryParseExact(s, "yyyy-MM-dd'T'HH:mm:ss'UTC'z", CultureInfo.InvariantCulture,
                          DateTimeStyles.AdjustToUniversal, out dt))
{
    Console.WriteLine(dt);
}

which gives you 2019-09-23 07:34:00 as a DateTime and which has Utc as a Kind property.

As a solution for DateTimeOffset - since your string has a UTC Offset value you should consider to parse with this rather than Datetime -, as Matt commented, you can use it's .DateTime property to get it's data like;

var s = "2019-09-23T08:34:00UTC+1";
DateTimeOffset dto;
if(DateTimeOffset.TryParseExact(s, "yyyy-MM-dd'T'HH:mm:ss'UTC'z", CultureInfo.InvariantCulture,
                          DateTimeStyles.None, out dto))
{
    Console.WriteLine(dto.DateTime);
}

which gives you the same result DateTime but Unspecified as a .Kind property.

But, again, I strongly suggest you to fix your input first.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

Use TryParseExact to convert the string to datetime. Here is the sample code to covert the given format(s) to datetime

private static DateTime ParseDate(string providedDate) {
    DateTime validDate;
    string[] formats = {
        "yyyy-MM-ddTHH:mm:ss"
    };
    var dateFormatIsValid = DateTime.TryParseExact(
    providedDate, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out validDate);

    return dateFormatIsValid ? validDate: DateTime.MinValue;
}

Then, use this function to convert the string. I am replacing UTC+1 to empty string

static void Main(string[] args) {
    string strdatetime = "2019-09-23T08:34:00UTC+1";
    DateTime dateTime = ParseDate(strdatetime.Replace("UTC+1", ""));

    Console.WriteLine(dateTime);
}
Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
Krishna Varma
  • 4,238
  • 2
  • 10
  • 25
  • Thanks Krishna this works with the exception of not accounting for the +1 in the time. How would I make sure that the time is accurate? Reason being is this is a tracking application and I'm parsing the ETA of deliveries so the UTA+1 is important – Sean Davies Nov 01 '19 at 14:28
  • @SeanDavies you can use `ParseDate(strdatetime.Replace("UTC+1", "")).ToUniversalTime()` to convert the date to UTC time. Or you can simply add 1 hour `ParseDate(strdatetime.Replace("UTC+1", "")).AddHours(1)` – Krishna Varma Nov 01 '19 at 14:31