Yogesh Yogesh - 2 months ago 13
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.

String.Insert(2,':');


Is there any better way to do the same?

Answer

Use below code

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