Is there a daylight savings check in Swift?

前端 未结 2 1450
抹茶落季
抹茶落季 2020-12-18 11:18

I need to check to see if the current date is during daylight savings time. In pseudocode that would be like this:

let date = NSDate()

if date.isDaylightSav         


        
2条回答
  •  悲哀的现实
    2020-12-18 11:58

    Swift 4.0 or later

    You can check a date isDaylightSavingTime in two ways by time zone identifier or abbreviation.

    let timeZone = TimeZone(identifier: "America/New_York")!
    if timeZone.isDaylightSavingTime(for: Date()) {
       print("Yes, daylight saving time at a given date")        
    }
    
    let timeZone = TimeZone(abbreviation: "EST")!
    if timeZone.isDaylightSavingTime(for: Date()) {
       print("Yes, daylight saving time at a given date")     
    } 
    

提交回复
热议问题