-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
  • 39,551
  • 56
  • 175
  • 291
K.Z
  • 5,201
  • 25
  • 104
  • 240

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,455
  • 5
  • 42
  • 71