-1

I need to convert UTC dateTime to local I have tried parse but unable to do so?

public class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("UTC DateTime Convertor....!");

        var giveDateTime = "22-06-2021T14:10:45.000";
        var utcGivenDateTime = DateTime.Parse(giveDateTime);???
        var localConvertedTime ???


        Console.WriteLine("Given Date: ", giveDateTime);
        Console.WriteLine("Given UTC DateTime ", utcGivenDateTime);
        Console.WriteLine("Converted Local Time ", localConvertedTime);


        Console.ReadLine();
    }
}
Uwe Keim
  • 38,279
  • 56
  • 171
  • 280
K.Z
  • 4,853
  • 23
  • 88
  • 206

1 Answers1

1

To convert to a local time, you need the timezone you want to convert it to:

var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central Brazilian Standard Time");
    
var localConvertedTime = TimeZoneInfo.ConvertTimeFromUtc(utcGivenDateTime, timeZoneInfo);

Unfortunately, native timezones are currently system specific. If you need to be system agnostic, you might want to look at third party libraries like NodaTime.

Ortiga
  • 8,093
  • 5
  • 42
  • 66