Yogesh Yogesh - 1 year ago 138
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?

Answer Source

Use below code

string time = "800";
string s1= DateTime.ParseExact(time.PadLeft(4, '0') , "HHmm", CultureInfo.InvariantCulture).ToString("HH:mm");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download