Je crée un compte à rebours à une date spécifique en utilisant NSDate
et NSDateFormatter
. Le seul problème est que lorsque je cours le code avec un format différent, la date est mauvaise. Voici le code original qui fonctionne très bien.NSDate Sorties Mauvaise date
- (void)updateCountdown {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd"];
NSDate *startingDate = [dateFormatter dateFromString:@"2014-10-08"];
NSDate *endingDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:startingDate toDate:endingDate options:0];
NSInteger days = [dateComponents day];
NSInteger months = [dateComponents month];
NSInteger years = [dateComponents year];
NSInteger hours = [dateComponents hour];
NSInteger minutes = [dateComponents minute];
NSInteger seconds = [dateComponents second];
NSString *countdownText = [NSString stringWithFormat:@"%ld Years %ld Months %ld Days %ld Hours %ld Minutes %ld Seconds", (long)days, (long)months, (long)years, (long)hours, (long)minutes, (long)seconds];
_countdown.text = countdownText;
[self performSelector:@selector(updateCountdown) withObject:nil afterDelay:1];
}
Mais que je veux supprimer les années donc je modifier le code comme ceci:
- (void)updateCountdown {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd"];
NSDate *startingDate = [dateFormatter dateFromString:@"2014-10-08"];
NSDate *endingDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:startingDate toDate:endingDate options:0];
NSInteger days = [dateComponents day];
NSInteger months = [dateComponents month];
NSInteger hours = [dateComponents hour];
NSInteger minutes = [dateComponents minute];
NSInteger seconds = [dateComponents second];
NSString *countdownText = [NSString stringWithFormat:@"%ld Months %ld Days %ld Hours %ld Minutes %ld Seconds", (long)days, (long)months, (long)hours, (long)minutes, (long)seconds];
_countdown.text = countdownText;
[self performSelector:@selector(updateCountdown) withObject:nil afterDelay:1];
}
Maintenant, tout d'un coup le dat est sali par un mois entier. Comment puis-je résoudre ce problème?
Aussi pourquoi est la sortie 0 ans -1 mois 0 jours -2 heures .. Comme pourquoi y at-il (-) là? Merci!
duh! Merci beaucoup! Savez-vous également pourquoi la sortie est -1 Jours -2 Heures -1 Minutes? Comme tu sais pourquoi les négatifs sont là? Merci beaucoup! – Nicholas
bien que les négatifs se rapportent probablement à être un compte à rebours. Donc, si je compte sur ce qui était hier, ce serait -1 jour, etc. Regardez votre code, peut-être vous comparez vos 2 dates pour comparer dans le mauvais ordre. C'est ma conjecture. – RyanTCB
Ouais c'est ce que je pensais aussi, sauf le fait que la date que j'ai est dans le futur! lol – Nicholas