Programming

C# float seconds to time format | C# 시간 초 값을 시간 형식으로 얻어 오기

DragonTory 2021. 11. 1. 16:50
반응형

C# float seconds to time format 

C# 시간 초 값을 시간 형식으로 얻어 오기 

using System;

1
Time.text  = TimeSpan.FromSeconds(CurrentTime).ToString(@"mm\:ss");
cs

 

Input : 90.0

Output : 01:30

 

TimSpan.FromSeconds()를 이용 하여 ToString 할 때 Format을 지정 하여 변환 할 수 있다.

dd:mm:ss

 

주의)  ToString("dd:mm:ss") 하면 에러 발생 함. 

에러 : FormatException: Input string was not in a correct format.

해결 : ToString("dd:mm:ss") -> ToString(@"dd\:mm\:ss") 하면 된다.

 

더 많은 TimeSpan Format Strings 인자에 대해서는 아래 참고.

https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-timespan-format-strings

반응형