Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Phương thức DateTime.IsDaylightSavingTime() trong C#

Phương thức DateTime.IsDaylightSavingTime() trong C# được dùng để chỉ ra thể hiện DateTime này có nằm trong phạm vi giờ mùa hè (daylight saving time) của múi giờ hiện tại hay không.

Cú pháp

Cú pháp như sau:

public bool IsDaylightSavingTime();

Ví dụ 1 - Kiểm tra ngày cụ thể

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.");
    }
}

Kết quả

FALSE: This instance of DateTime is within the daylight saving time range for the current time zone.

Ví dụ 2 - Kiểm tra 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.");
    }
}

Kết quả

FALSE: This instance of DateTime is within the daylight saving time range for the current time zone.