Oleg Gordiichuk Oleg Gordiichuk - 1 year ago 87
Swift Question

How to create NSDate that will be in DST mode

I would like to know how it is possible to create NSDate that will be in DST mode.

I am using method


This is from documentation:

This property contains the date of the next (after the current
instant) daylight saving time transition for the receiver. Depending
on the time zone of the receiver, the value of this property may
represent a change of the time zone's offset from GMT. Returns nil if
the time zone of the receiver does not currently observe daylight
saving time.

And than i validate if it is in DST mode:

I am using method:

This is from documentation:

YES if the receiver uses daylight saving time at aDate, otherwise NO.


NSDate *nextDstTransition = [[NSTimeZone localTimeZone] nextDaylightSavingTimeTransition];

BOOL isDstOnForOldDate = [[NSTimeZone localTimeZone] isDaylightSavingTimeForDate:nextDstTransition];

Unfortunately my
is not in DST. My assumptions are connected with time zone problem.

Could you share you option about creation of the
in DST mode.

Answer Source

If you read the documentation carefully: nextDaylightSavingTimeTransition: returns the next time when the DST changes after a given date, or nil if you are in time zone where DST never changes (either always on, or always off). If your date is in DST, then you are fine, otherwise you need to know when it changes.

NSDate* date = ...
if (! [[NSTimeZone localTimeZone] isDaylightSavingTimeForDate:date]) {
    NSDate* change = [[NSTimeZone localTimeZone] nextDaylightSavingTimeTransitionAfterDate:date];
    if (change != nil) {
        date = change;
    } else {
        // Tough, there is no DST in this time zone. 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download