Yogesh Yogesh - 26 days ago 9x
C# Question

Add colon to 24 hour time in C#?

I have time stored in Db as 800,1600 format in separate column. I want to add colon in time before giving it to UI. Currently I am calculating the size and zero if size is three and using insert to add colon after 2 characters.


Is there any better way to do the same?


Use below code

string time = "800";
string s1= DateTime.ParseExact(time.PadLeft(4, '0') , "HHmm", CultureInfo.InvariantCulture).ToString("HH:mm");