当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# DateTime.IsDaylightSavingTime()用法及代码示例


此方法用于指示DateTime的实例是否在当前时区的夏时制时间范围内。

用法: public bool IsDaylightSavingTime ();

返回值:如果Kind属性的值为Local或Unspecified,并且此DateTime实例的值在本地时区的夏令时范围内,则此方法返回;如果Kind为Utc,则返回false。


以下示例程序旨在说明DateTime.IsDaylightSavingTime()方法的使用:

示例1:

// C# program to demonstrate the 
// DateTime.IsDaylightSavingTime() 
// Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        // creating object of DateTime 
        DateTime date = new DateTime(2010, 1, 
                                1, 4, 0, 15); 
  
        // getting Typecode of date 
        // using IsDaylightSavingTime() method; 
        bool value = date.IsDaylightSavingTime(); 
  
        // checking the condition 
        if (value) 
            Console.WriteLine("Instance of DateTime is within the"
                               + " daylight saving time range for"+ 
                                        " the current time zone."); 
  
        else
            Console.WriteLine("Instance of DateTime is not within the"
                              + " daylight saving time range for the "+ 
                                                  "current time zone."); 
    } 
}
输出:

Instance of DateTime is not within the daylight saving time range for the current time zone.

示例2:

// C# program to demonstrate the 
// DateTime.IsDaylightSavingTime() 
// Method 
using System; 
using System.Globalization; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        // creating object of DateTime 
        DateTime date = new DateTime(1970, 1, 
                                 1, 4, 0, 15); 
  
        // getting Typecode of date 
        // using IsDaylightSavingTime() method; 
        bool value = date.IsDaylightSavingTime(); 
  
        // checking the condition 
        if (value) 
            Console.WriteLine("Instance of DateTime is within the"
                              + " daylight saving time range for "+ 
                                         "the current time zone."); 
  
        else
            Console.WriteLine("Instance of DateTime is not within the"
                              + " daylight saving time range for the "+ 
                                                 "current time zone."); 
    } 
}
输出:

Instance of DateTime is not within the daylight saving time range for the current time zone.

参考:



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 DateTime.IsDaylightSavingTime() Method in C#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。