Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimeSpan formatting to minutes ans seconds without hours

I need format TimeSpan to minutes and seconds(without hours), I looks to MSDN formatting but not found any example which I can reuse.

Example of what I need:

TimeSpan.FromSeconds(1) --> 00:01
TimeSpan.FromSeconds(60) --> 01:00
TimeSpan.FromSeconds(3600) --> 60:00
TimeSpan.FromSeconds(36000) --> 600:00

Any ideas which format possible use to convert TimeSpan to string in minutes:seconds format?

like image 415
Толя Avatar asked Jan 27 '26 18:01

Толя


1 Answers

Try this:

var timeSpan = TimeSpan.FromSeconds(3123);

var result = string.Format("{0:D2}:{1:D2}", (int) timeSpan.TotalMinutes, timeSpan.Seconds);

// result = "52:03"
like image 132
khellang Avatar answered Jan 29 '26 06:01

khellang