Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

วิธีแปลงวินาทีเป็นรูปแบบ ชั่วโมง:นาที:วินาที:มิลลิวินาที ใน C#

ใช้โครงสร้าง TimeSpan ซึ่งแสดงช่วงเวลา (Time Interval) และมี Method Static สำหรับสร้างจากหน่วยต่างๆ

เมธอดสร้าง TimeSpan

  • TimeSpan.FromSeconds(double)
  • TimeSpan.FromMinutes(double)
  • TimeSpan.FromHours(double)
  • TimeSpan.FromDays(double)
  • TimeSpan.FromMilliseconds(double)
  • TimeSpan.FromTicks(long)

ตัวอย่างพื้นฐาน

using System;

static void Main()
{
    double totalSeconds = 3752; // 1 ชม. 2 นาที 32 วินาที
    
    TimeSpan ts = TimeSpan.FromSeconds(totalSeconds);
    
    // Format ด้วย String.Format / ToString
    string formatted = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms",
        ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);
    
    Console.WriteLine(formatted); // 01h:02m:32s:000ms
    
    // หรือใช้ Custom Format String (TimeSpan รองรับ Custom Format ตั้งแต่ .NET 4+)
    string custom = ts.ToString(@"hh\:mm\:ss\:fff");
    Console.WriteLine(custom); // 01:02:32:000
}

ผลลัพธ์

01h:02m:32s:000ms
01:02:32:000

ตัวอย่างเพิ่มเติม

static void Main()
{
    var testCases = new[] { 6.0, 65.5, 3600, 3661.123, 90061 };
    
    foreach (var seconds in testCases)
    {
        TimeSpan ts = TimeSpan.FromSeconds(seconds);
        Console.WriteLine($"{seconds,8} sec = {ts:hh\\:mm\\:ss\\.fff} = {FormatTimeSpan(ts)}");
    }
}

static string FormatTimeSpan(TimeSpan ts)
{
    // รองรับเกิน 24 ชั่วโมง (Days)
    int totalHours = (int)ts.TotalHours;
    return $"{totalHours:D2}h:{ts.Minutes:D2}m:{ts.Seconds:D2}s:{ts.Milliseconds:D3}ms";
}

ผลลัพธ์

     6 sec = 00:00:06.000 = 00h:00m:06s:000ms
    65.5 sec = 00:01:05.500 = 00h:01m:05s:500ms
  3600 sec = 01:00:00.000 = 01h:00m:00s:000ms
 3661.123 sec = 01:01:01.123 = 01h:01m:01s:123ms
 90061 sec = 25:01:01.000 = 25h:01m:01s:000ms

Format Strings สำหรับ TimeSpan

Format ความหมาย ตัวอย่าง (01:02:03.456)
h / hh Hours (0-23) 1 / 01
m / mm Minutes (0-59) 2 / 02
s / ss Seconds (0-59) 3 / 03
f..fffffff Fractional Seconds (1-7 digits) 4 / 45 / 456 / 4560000
F..FFFFFFF Fractional Seconds (Trim trailing zeros) 4 / 45 / 456 / (empty)
d / dd Days (Total Days) 0 / 00

Note: ใช้ Backslash \ Escape Colon : ใน Format String (เช่น hh\:mm\:ss)

Extension Method แบบ Reusable

public static class TimeSpanExtensions
{
    public static string ToReadableString(this TimeSpan ts, bool includeMilliseconds = true)
    {
        int totalHours = (int)ts.TotalHours;
        string baseStr = $"{totalHours:D2}h:{ts.Minutes:D2}m:{ts.Seconds:D2}s";
        return includeMilliseconds ? $"{baseStr}:{ts.Milliseconds:D3}ms" : baseStr;
    }
    
    public static string ToShortString(this TimeSpan ts)
    {
        if (ts.TotalDays >= 1) return $"{(int)ts.TotalDays}d {ts.Hours}h {ts.Minutes}m";
        if (ts.TotalHours >= 1) return $"{ts.Hours}h {ts.Minutes}m {ts.Seconds}s";
        if (ts.TotalMinutes >= 1) return $"{ts.Minutes}m {ts.Seconds}s";
        return $"{ts.Seconds}.{ts.Milliseconds:D3}s";
    }
}

การใช้งาน

TimeSpan duration = TimeSpan.FromSeconds(3752);
Console.WriteLine(duration.ToReadableString()); // 01h:02m:32s:000ms
Console.WriteLine(duration.ToShortString());    // 1h 2m 32s