2016-10-26 46 views
0

我現在很想知道如何從Swift 3中的時間選擇器中選擇時間減去3,10和20分鐘。因此,例如,當用戶選擇事件的開始時間,以便能夠接收通知3時,從選取器時間值中選擇10或20分鐘。我曾嘗試從時間選擇器值減去分鐘數?

let value:TimeInterval = 1187.5 let periodComponents = startTimePicker.date.addingTimeInterval(-value)

爲20分鐘前, 但我得到的價值2小時背後(可能是控制檯只顯示我的GTM時間)。是否有可能安排來自「periodComponens」的通知在每週特定日期的選定時間內每週重複一次?或者我應該使用其他減法方法?

回答

-1

沒有什麼不對您的代碼。 xcode控制檯默認打印出GMT日期時間。如果您使用NSDateFormatter來轉換成NSDateNSString打印出來,結果會如你預期

如果使用UILocalNotification並希望安排其每週解僱你可以將其提供的屬性repeatInterval設置爲NSCalendarUnitWeekOfYear

例如:

UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
NSDate *fireTime; // Your desired date time. 

localNotif.fireDate = fireTime; 
localNotif.repeatInterval = NSCalendarUnitWeekOfYear; 
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
+0

對於那些誰downvoted我的回答:如果你downvoted,因爲它在迅速不,我只是盡我所能來幫助。僅僅因爲我沒有迅速的經驗,並不意味着我不能幫助別人解決他們的問題。 –

0

使用內置Calendar類:

let threeMinutesEarlier = Calendar.current.date(byAdding: .minute, value: -3, to: myPicker.date)