โครงสร้าง TimeSpan แสดงช่วงเวลา (Duration) สามารถจัดรูปแบบการแสดงผลได้ด้วย Format String โดยใช้ ToString() หรือ String.Format() รูปแบบมาตรฐานคือ hh\\:mm\\:ss (ต้อง Escape colon ด้วย Backslash)
รูปแบบ Format String สำหรับ TimeSpan
| Format | คำอธิบาย | ตัวอย่าง (9 ชม. 15 นาที 30 วินาที) |
|---|---|---|
hh\\:mm\\:ss | ชั่วโมง:นาที:วินาที (12 ชม.) | 09:15:30 |
HH\\:mm\\:ss | ชั่วโมง:นาที:วินาที (24 ชม.) | 09:15:30 |
h\\:mm\\:ss | ชั่วโมงไม่มีศูนย์นำหน้า | 9:15:30 |
hh\\:mm | ชั่วโมง:นาที (ไม่มีวินาที) | 09:15 |
d\\.hh\\:mm\\:ss | วัน.ชั่วโมง:นาที:วินาที | 1.09:15:30 |
c | Constant format (มาตรฐาน) | 1.09:15:30.0000000 |
g | General format (สั้น) | 1:09:15:30 |
G | General format (ยาว) | 1:09:15:30.0000000 |
โค้ดตัวอย่างสมบูรณ์
using System;
public class Demo
{
public static void Main()
{
TimeSpan ts = new TimeSpan(9, 15, 30); // 9 ชม. 15 นาที 30 วินาที
Console.WriteLine($"TimeSpan ต้นฉบับ: {ts}");
Console.WriteLine($"TotalDays: {ts.TotalDays:F4}");
Console.WriteLine($"TotalHours: {ts.TotalHours:F4}");
Console.WriteLine($"TotalMinutes: {ts.TotalMinutes:F2}");
Console.WriteLine($"TotalSeconds: {ts.TotalSeconds:F0}");
Console.WriteLine("\n--- รูปแบบการแสดงผล ---");
Console.WriteLine($"hh\\:mm\\:ss : {ts.ToString(@"hh\:mm\:ss")}"); // 09:15:30
Console.WriteLine($"HH\\:mm\\:ss : {ts.ToString(@"HH\:mm\:ss")}"); // 09:15:30
Console.WriteLine($"h\\:mm\\:ss : {ts.ToString(@"h\:mm\:ss")}"); // 9:15:30
Console.WriteLine($"hh\\:mm : {ts.ToString(@"hh\:mm")}"); // 09:15
Console.WriteLine($"d\\.hh\\:mm\\:ss : {ts.ToString(@"d\.hh\:mm\:ss")}"); // 0.09:15:30
Console.WriteLine($"c (Constant) : {ts.ToString("c")}"); // 0.09:15:30.0000000
Console.WriteLine($"g (General) : {ts.ToString("g")}"); // 0:09:15:30
Console.WriteLine($"G (General long): {ts.ToString("G")}"); // 0:09:15:30.0000000
// ใช้กับ String.Format
Console.WriteLine($"\nString.Format: {{0:hh\\:mm\\:ss}} = {string.Format("{0:hh\\:mm\\:ss}", ts)}");
// TimeSpan ที่เกิน 24 ชั่วโมง
TimeSpan longTs = new TimeSpan(2, 10, 30, 45); // 2 วัน 10 ชม. 30 นาที 45 วินาที
Console.WriteLine($"\nTimeSpan ยาว: {longTs}");
Console.WriteLine($"d\\.hh\\:mm\\:ss : {longTs.ToString(@"d\.hh\:mm\:ss")}"); // 2.10:30:45
Console.WriteLine($"TotalHours : {longTs.TotalHours:F2} ชั่วโมง"); // 58.51
}
}
ผลลัพธ์
TimeSpan ต้นฉบับ: 09:15:30
TotalDays: 0.3854
TotalHours: 9.2583
TotalMinutes: 555.50
TotalSeconds: 33330
--- รูปแบบการแสดงผล ---
hh\:mm\:ss : 09:15:30
HH\:mm\:ss : 09:15:30
h\:mm\:ss : 9:15:30
hh\:mm : 09:15
d\.hh\:mm\:ss : 0.09:15:30
c (Constant) : 0.09:15:30.0000000
g (General) : 0:09:15:30
G (General long): 0:09:15:30.0000000
String.Format: {0:hh\:mm\:ss} = 09:15:30
TimeSpan ยาว: 2.10:30:45
d\.hh\:mm\:ss : 2.10:30:45
TotalHours : 58.51 ชั่วโมง
การจัดรูปแบบแบบกำหนดเอง (Custom)
TimeSpan ts = new TimeSpan(25, 30, 45); // 1 วัน 1 ชม. 30 นาที 45 วินาที
// แสดงเฉพาะชั่วโมงทั้งหมด (รวมวัน)
Console.WriteLine($"ชั่วโมงทั้งหมด: {ts.TotalHours:F0}:{ts.Minutes:D2}:{ts.Seconds:D2}");
// ผลลัพธ์: 25:30:45
// หรือใช้ Custom Format
string custom = $"{ts.Days}d {ts.Hours}h {ts.Minutes}m {ts.Seconds}s";
Console.WriteLine(custom); // 1d 1h 30m 45s
การ Parse TimeSpan จากสตริง
// Parse จากรูปแบบมาตรฐาน
TimeSpan ts1 = TimeSpan.Parse("09:15:30");
TimeSpan ts2 = TimeSpan.Parse("1.09:15:30"); // 1 วัน 9:15:30
// Parse แบบปลอดภัย
if (TimeSpan.TryParse("09:15:30", out TimeSpan ts3))
Console.WriteLine($"Parse สำเร็จ: {ts3}");
ข้อควรระวัง
- ใน Format String ต้อง Escape colon (:) ด้วย Backslash (\\:) หรือใช้ @"" verbatim string
hh= 12-hour format,HH= 24-hour format (แต่ TimeSpan ไม่มี AM/PM)TotalHoursรวมวันด้วย ส่วนHoursแค่ 0-23- สำหรับระยะเวลายาวนาน ควรใช้
d\\.hh\\:mm\\:ssหรือคำนวณจาก TotalHours เอง