Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert any time format in milliseconds with C#

I have a time in this format: 00:02:13,512 Is there a method in C# that can convert this time to milliseconds (and vice versa) or must i do it manually?

like image 539
As As Avatar asked Oct 17 '25 23:10

As As


1 Answers

Use TimeSpan to store this information. You can use TimeSpan.ParseExact like:

TimeSpan ts = TimeSpan.ParseExact("00:02:13,512", 
                                  @"hh\:mm\:ss\,fff", 
                                  CultureInfo.InvariantCulture);

You can get TotalMilliseconds using TimeSpan.TotalMilliseconds property:

var totlaMilliseconds = ts.TotalMilliseconds;

This would give you back 133512.0 if you just need to Millisecond part then you can use ts.Milliseconds; which would give you 512

like image 88
Habib Avatar answered Oct 19 '25 14:10

Habib



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!