Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Milliseconds to date format in C#?

In C# how can I convert Unix-style timestamp to yyyy-MM-ddThh:mm:ssZ?

like image 742
danny Avatar asked Sep 07 '11 16:09

danny


People also ask

How are milliseconds represented in date format?

Usually we display time in in 12 hour format hh:mm:aa format (e.g. 12:30 PM) or 24 hour format HH:mm (e.g. 13:30), however sometimes we also want to show the milliseconds in the time. To show the milliseconds in the time we include “SSS” in the pattern which displays the Milliseconds.

How do you convert milliseconds?

To convert a second measurement to a millisecond measurement, multiply the time by the conversion ratio. The time in milliseconds is equal to the seconds multiplied by 1,000.


1 Answers

Start by converting your milliseconds to a TimeSpan:

var time = TimeSpan.FromMilliseconds(milliseconds); 

Now, in .NET 4 you can call .ToString() with a format string argument. See http://msdn.microsoft.com/en-us/library/system.timespan.tostring.aspx

In previous versions of .NET, you'll have to manually construct the formatted string from the TimeSpan's properties.

like image 190
Jay Avatar answered Sep 23 '22 00:09

Jay