-2
我想要倒計時,顯示日,小時,M和S.第二次被調用一次。如何在objc中比較時間
- (void)changetime {
NSCalendar *cal = [NSCalendar currentCalendar];//定義一個NSCalendar對象
NSDate *today = [NSDate date];//得到當前時間
unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *weekdayComponents =[cal components:(unitFlags) fromDate:self.overDate];
NSDate *todate = [cal dateFromComponents:weekdayComponents];
NSDateComponents *d = [cal components:unitFlags fromDate:today toDate:todate options:0];
NSInteger days=[d year]*365+[d month]*31+[d day];
if ([self.overDate earlierDate:[NSDate date]]==self.overDate) {
[myTimer invalidate];
myTimer=nil;
self.labdate.text = [NSString stringWithFormat:@"%d天%d時%d分%d秒",days, [d hour], [d minute], [d second]];
return;
}
self.labdate.text = [NSString stringWithFormat:@"%d天%d時%d分%d秒",days, [d hour], [d minute], [d second]];
}
myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changetime) userInfo:nil repeats:YES];
self.overDate=[wedding date];//this is a future time