UTC Offset format conversion

Jayanth Bondi

New member
Joined
May 28, 2022
Messages
2
Programming Experience
1-3
Hello Team,
I have UtcOffset value as mentioned below.
UTC-0500 (hhmm format).

I want to convert this to hh:mm:ss i.e. UTC-05:00:00.

Could you please help me out.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,466
Location
Sydney, Australia
Programming Experience
10+
There would be other variations but here's one option:
C#:
var str = "UTC-0500";

str = str.Substring(0, 4) + TimeSpan.ParseExact(str.Substring(4), "hhmm", null).ToString(@"hh\:mm\:ss");

Console.WriteLine(str);
 
Top Bottom