1

This is a working function who rounds off the minutes to nearest five. What I need is it to return the time in a specific timezone, so whetheryou're in timezoon +1, +5 or whatever, it must return timezone +1. Anyone knows how i can solve this?

func SkipToEvenFiveMinutes(date:NSDate) -> NSDate!
{

    let componentMask : NSCalendarUnit = ([NSCalendarUnit.Year , NSCalendarUnit.Month , NSCalendarUnit.Day , NSCalendarUnit.Hour ,NSCalendarUnit.Minute, NSCalendarUnit.Second+NSTimeZone setDefaultTimeZone:UTC+1])
    let components = NSCalendar.currentCalendar().components(componentMask, fromDate: date)
    let current = components.minute * 60
    let currentSeconds = current + components.second

    let diff = currentSeconds % (60 * 5);
    let difference = Double(diff)
    if (difference > 60.0 * 2.5) {
        components.second += 60 * 5 - diff
    } else {
        components.second -= diff
    }

    return NSCalendar.currentCalendar().dateFromComponents(components)!

}

1 Answers1

1

Dates does not have time zones. So there is nothing to convert. Calendars and formatters have time zones.

So, what you should do, is to select the right calendar for the different time zone. Then you can get the units for this time zone using the second calendar object.

Community
  • 1
  • 1
Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50