Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a daylight savings check in Swift?

Tags:

dst

swift

nsdate

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.isDaylightSavingsTime {

    print("Success")

}

I haven't been able to find the solution to this anywhere on the internet.

like image 711
bkSwifty Avatar asked Dec 30 '15 16:12

bkSwifty


People also ask

Does Daylight Savings affect flight check in?

​Daylight savings time will affect local scheduled flight times the same way it will affect your live television schedule. It will affect apparent flight duration if the departure and arrival airports switch to DST on different days of the year, but actual time in the air will remain the same.

How do you account for daylight savings time?

Clocks Back or Forward? “Spring forward, fall back” is one of the little sayings used to remember which way to set your watch. You set your clock forward one hour in the spring when DST starts (= lose 1 hour), and back one hour when DST ends in the fall (= regain 1 hour).

Do I turn my clock back or forward in November?

As is often said twice a year on the occasion of Daylight Saving Time's start and stop, “spring forward, fall back,” as clocks move forward an hour in March and back an hour in November.

Which time zone does not follow day light saving?

There are two time zones that do not observe daylight saving time and that have the same UTC offset (-06:00): (UTC-06:00) Central America. (UTC-06:00) Saskatchewan.


1 Answers

An NSDate alone represents an absolute point in time. To decide if a date is during daylight savings time or not it needs to be interpreted in the context of a time zone.

Therefore you'll find that method in the NSTimeZone class and not in the NSDate class. Example:

let date = NSDate()

let tz = NSTimeZone.localTimeZone()
if tz.isDaylightSavingTimeForDate(date) {

}

Update for Swift 3/4:

let date = Date()

let tz = TimeZone.current
if tz.isDaylightSavingTime(for: date) {
    print("Summertime, and the livin' is easy ... 🎶")
}
like image 127
Martin R Avatar answered Sep 22 '22 04:09

Martin R