Calendoscopio
Calendoscopio

Reputation: 1

TryParseExact does not convert a string with timezone

I have a string "2023-04-13T07:03:20-03:00", and I want to convert to DateTime with a specific format "yyyy-MM-ddTHH:mm:ss", so i'm using the TryParseExact method, but I just receive false as return.

This is the code I'm working on. When the variable comes without timezone, the conversion happens normally, however with the timezone no format works.

In the documentation I didn't find anything that says that this way I'm doing it is wrong.

I also tried setting the datestyle to AssumeUniversal, it didn't work.

If anyone can give me some clarification I would appreciate it.

using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
                    
public class Program
{
    public static void Main()
    {
        var formats = new[]{
            "o",
                        "s",
                        "t", "T",
                        "M/yy",
                        "dd-MM-yy",
        "yyyy-MM-ddTHH:mm:ss",};
        
        foreach(var format in formats)
        {
            DateTime dat1;
            DateTime dat2;
            Console.WriteLine(format);
            Console.WriteLine("");
            Console.WriteLine(DateTime.TryParseExact("2023-04-13T07:03:20-03:00", formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dat1) ? dat1.ToString(format) : "false");
            Console.WriteLine(DateTime.TryParseExact("2023-04-13T07:03:20", formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dat2) ? dat2.ToString(format) : "false");
            Console.WriteLine("");
            Console.WriteLine("--------------------");
        }
    }
}

The result:

o

false 2023-04-13T07:03:20.0000000


s

false 2023-04-13T07:03:20


t

false 7:03 AM


T

false 7:03:20 AM


M/yy

false 4/23


dd-MM-yy

false 13-04-23


yyyy-MM-ddTHH:mm:ss

false 2023-04-13T07:03:20


Upvotes: -1

Views: 121

Answers (2)

Mehran
Mehran

Reputation: 9

The format "yyyy-MM-ddTHH:mm:ss" that you are using to parse the date and time string "2023-04-13T07:03:20-03:00" is correct. However, the issue you are facing is related to the timezone information in the input string.

The timezone information "-03:00" is not recognized by the TryParseExact method, which expects a timezone offset in the format "+/-HH:mm". To fix this, you can modify the input string to replace the colon ":" with an empty string before attempting to parse it.

Here's an updated version of your code that should work:

using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;

public class Program
{
    public static void Main()
    {
        var formats = new[]{
            "o",
            "s",
            "t", "T",
            "M/yy",
            "dd-MM-yy",
            "yyyy-MM-ddTHH:mm:ss",
        };

        var input = "2023-04-13T07:03:20-03:00".Replace(":", "");
        
        foreach(var format in formats)
        {
            DateTime dat1;
            Console.WriteLine(format);
            Console.WriteLine("");
            Console.WriteLine(DateTime.TryParseExact(input, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dat1) ? dat1.ToString(format) : "false");
            Console.WriteLine("");
            Console.WriteLine("--------------------");
        }
    }
}

In the updated code, we first replace the colon ":" in the input string with an empty string. Then we iterate through the list of formats and try to parse the input string using each format. The output should now show the expected date and time value in the "yyyy-MM-ddTHH:mm:ss" format.

Upvotes: 0

Misha Zaslavsky
Misha Zaslavsky

Reputation: 9626

You should include the timezone offset in the format. You can do it using "K".

var formats = new[] { "yyyy-MM-ddTHH:mm:ssK" };

Upvotes: 1

Related Questions