เมธอด DateTime.IsDaylightSavingTime() ใน C# ใช้สำหรับระบุว่าอินสแตนซ์ DateTime นี้อยู่ในช่วงเวลาประหยัดแสงแดด (daylight saving time) สำหรับโซนเวลาปัจจุบันหรือไม่
ไวยากรณ์
ไวยากรณ์มีดังนี้:
public bool IsDaylightSavingTime ();
ตัวอย่าง
ตัวอย่างการตรวจสอบวันที่เฉพาะเจาะจง:
using System;
public class Demo {
public static void Main() {
DateTime d = new DateTime(2019, 10, 11, 7, 10, 40);
bool res = d.IsDaylightSavingTime();
if (res)
Console.WriteLine("TRUE: This instance of DateTime is within the daylight saving time range for the current time zone.");
else
Console.WriteLine("FALSE: This instance of DateTime is within the daylight saving time range for the current time zone.");
}
}
ผลลัพธ์
โค้ดจะแสดงผลดังนี้:
FALSE: This instance of DateTime is within the daylight saving time range for the current time zone.
ตัวอย่างเพิ่มเติม
ตัวอย่างกับ DateTime.Now:
using System;
public class Demo {
public static void Main() {
DateTime d = DateTime.Now;
bool res = d.IsDaylightSavingTime();
if (res)
Console.WriteLine("TRUE: This instance of DateTime is within the daylight saving time range for the current time zone.");
else
Console.WriteLine("FALSE: This instance of DateTime is within the daylight saving time range for the current time zone.");
}
}
ผลลัพธ์
โค้ดจะแสดงผลดังนี้ (ขึ้นอยู่กับวันที่และโซนเวลา):
FALSE: This instance of DateTime is within the daylight saving time range for the current time zone.
หมายเหตุ: ผลลัพธ์ขึ้นอยู่กับโซนเวลาของระบบและวันที่ที่ตรวจสอบ