0

How can I reduce 5 or 10 minutes from current time and then store it as date format in Swift 2.0 ?

let now = NSDate()
var reducedTime = ???? \\ Here I want 10 minutes reduced from current time.

If current time is 02:20:00, then reducedTime should be 02:10:00 How can I do this in a simplest way ???

AAA
  • 1,957
  • 4
  • 23
  • 42
  • Have you read the documentation - https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/#//apple_ref/doc/uid/20000188-SW8 ? Hint: adding a negative number is the same as subtraction – Paulw11 Oct 08 '15 at 21:01
  • That was just the first link that Google through up. Since it is Cocoa foundation it makes no difference but I have changed the link. – Paulw11 Oct 08 '15 at 21:05

1 Answers1

3

You can use calendar method dateByAddingUnit and subtract 10 minutes fro the date.

let now = NSDate()

let reducedTime = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)?.dateByAddingUnit(.Minute, value: -10, toDate: now, options: NSCalendarOptions())
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571