简体   繁体   中英

How can I convert seconds to hours minutes and seconds?

How can I convert seconds into a custom Time struct?

struct Time
{
    public int hr;
    public int min;
    public int sec;
}

Is there a library function that can do this or do I have to implememt a function myself?

Use the TimeSpan structure.

TimeSpan myTimeSpan = TimeSpan.FromSeconds(9755);
Console.WriteLine("Hours: " + myTimeSpan.Hours); // 2
Console.WriteLine("Minutes: " + myTimeSpan.Minutes); // 42
Console.WriteLine("Seconds: " + myTimeSpan.Seconds); // 35

Verified with random site I found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM