我试图比较NSDates,主要是时间,以确定应该在即将推出的方法中返回哪个字符串,但首先是设置我正在比较的日期的调用。
- (id)init {
if ( self = [ super init ] ) {
self.tripTime = 0.0;
self.timer =
[ [ BestRouteTimer alloc ] init ];
self.morningBeforeTrafic = [ self dateWithHour:0 andMinute:0 ];
self.morningTrafic = [ self dateWithHour:6 andMinute:30 ];
self.morningAfterTrafic = [ self dateWithHour:9 andMinute:30 ];
self.afternoon = [ self dateWithHour:12 andMinute:0 ];
self.eveningTrafic = [ self dateWithHour:15 andMinute:30 ];
self.eveningAfterTrafic = [ self dateWithHour:18 andMinute:30 ];
self.timeOfDay = [ self determineTimeofDayWithTime:[NSDate date ] ] ;
}
return self;
}
比较方法
// Will take the time passed in and return a string
// to represent the time of day
- (NSString *) determineTimeofDayWithTime:(NSDate *)time {
NSLog(@"the time of day is \n%@",
//[ self standardizeDate:time ].description );
[ time description ] );
NSLog(@"The before morning trafic is %@",
// [ self standardizeDate:self.morningBeforeTrafic ].description );
[self.morningBeforeTrafic description ]);
NSLog(@"The morning trafic is %@",
// [ self standardizeDate:self.morningTrafic ].description );
[self.morningTrafic description ]);
NSLog(@"The after morning trafic is %@",
//[ self standardizeDate:self.morningAfterTrafic ].description );
[self.morningAfterTrafic description ]);
NSLog(@"The afternoon trafic is %@",
//[ self standardizeDate:self.afternoon ].description );
[self.afternoon description ]);
NSLog(@"The evening trafic is %@",
//[ self standardizeDate:self.afternoon ].description );
[self.eveningTrafic description ]);
NSLog(@"The evening after trafic is %@",
//[ self standardizeDate:self.eveningAfterTrafic ].description );
[self.eveningAfterTrafic description ]);
if ( ( [ time compare:self.morningBeforeTrafic ] == NSOrderedAscending ||
[ self.morningBeforeTrafic compare:time ] == NSOrderedSame ) &&
[ time compare:self.morningTrafic ] == NSOrderedAscending ) {
return @"Morning, before traffic.";
}
else if ( ( [ self.morningTrafic compare:time ] == NSOrderedAscending ||
[ self.morningTrafic compare:time ] == NSOrderedSame ) &&
[ time compare:self.morningAfterTrafic ] == NSOrderedAscending ) {
return @"Morning Traffic!";
}
else if ( ( [ self.morningAfterTrafic compare:time ] == NSOrderedAscending ||
[ self.morningAfterTrafic compare:time ] == NSOrderedSame ) &&
[ time compare:self.afternoon ] == NSOrderedAscending ) {
return @"Morning, after traffic ";
}
else if ( ( [ time compare:self.afternoon ] == NSOrderedAscending ||
[ self.afternoon compare:time ] == NSOrderedSame ) &&
[ self.eveningTrafic compare:time ] == NSOrderedAscending ) {
return @"Afternoon";
}
else if ( ( [ self.eveningTrafic compare:time ] == NSOrderedAscending ||
[ self.eveningTrafic compare:time ] == NSOrderedSame ) &&
[ time compare:self.eveningAfterTrafic ] == NSOrderedAscending ) {
return @"Evening Traffic!";
}
else
return @"Evening, after traffic";
}
最后,这是生成日期的方法
- (NSDate *)dateWithHour:(int)hour andMinute:(int)minutes {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar ];
[ calendar setTimeZone:[ NSTimeZone localTimeZone ] ];
NSDate *today = [NSDate date];
NSDateComponents *todayComps =
[calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSMinuteCalendarUnit | NSHourCalendarUnit |NSTimeZoneCalendarUnit) fromDate:today ];
todayComps.timeZone = [ NSTimeZone localTimeZone ];
todayComps.minute = minutes;
todayComps.hour = hour;
NSDate *newDate = [ calendar dateFromComponents:todayComps ];
NSLog(@"The time zone is %@", todayComps.timeZone.description );
NSLog(@"The new date is %@", newDate.description );
return newDate;
}
这是日志声明
2013-11-12 10:29:16.609 BestRouteFinal[2768:60b] The time zone is America/Denver (MST) offset -25200
2013-11-12 10:29:16.611 BestRouteFinal[2768:60b] The new date is 2013-11-12 07:00:00 +0000
2013-11-12 10:29:16.613 BestRouteFinal[2768:60b] The time zone is America/Denver (MST) offset -25200
2013-11-12 10:29:16.615 BestRouteFinal[2768:60b] The new date is 2013-11-12 13:30:00 +0000
2013-11-12 10:29:16.617 BestRouteFinal[2768:60b] The time zone is America/Denver (MST) offset -25200
2013-11-12 10:29:16.619 BestRouteFinal[2768:60b] The new date is 2013-11-12 16:30:00 +0000
2013-11-12 10:29:16.622 BestRouteFinal[2768:60b] The time zone is America/Denver (MST) offset -25200
2013-11-12 10:29:16.623 BestRouteFinal[2768:60b] The new date is 2013-11-12 19:00:00 +0000
2013-11-12 10:29:16.627 BestRouteFinal[2768:60b] The time zone is America/Denver (MST) offset -25200
2013-11-12 10:29:16.629 BestRouteFinal[2768:60b] The new date is 2013-11-12 22:30:00 +0000
2013-11-12 10:29:16.632 BestRouteFinal[2768:60b] The time zone is America/Denver (MST) offset -25200
2013-11-12 10:29:16.633 BestRouteFinal[2768:60b] The new date is 2013-11-13 01:30:00 +0000
2013-11-12 10:29:16.635 BestRouteFinal[2768:60b] the time of day is
2013-11-12 17:29:16 +0000
2013-11-12 10:29:16.637 BestRouteFinal[2768:60b] The before morning trafic is 2013-11-12 07:00:00 +0000
2013-11-12 10:29:16.639 BestRouteFinal[2768:60b] The morning trafic is 2013-11-12 13:30:00 +0000
2013-11-12 10:29:16.641 BestRouteFinal[2768:60b] The after morning trafic is 2013-11-12 16:30:00 +0000
2013-11-12 10:29:16.642 BestRouteFinal[2768:60b] The afternoon trafic is 2013-11-12 19:00:00 +0000
2013-11-12 10:29:16.644 BestRouteFinal[2768:60b] The evening trafic is 2013-11-12 22:30:00 +0000
2013-11-12 10:29:16.646 BestRouteFinal[2768:60b] The evening after trafic is 2013-11-13 01:30:00 +0000
正如您所看到的,时间已经过了7个小时。我是ios的新手,这是到目前为止我遇到的最复杂的事情。我搜索过很多帖子并尝试实施各种解决方案,但没有运气。我已经待了一个多星期了,不知道我还能尝试什么。
答案 0 :(得分:0)
NSDate
没有时区的概念,因此无论您何时进行注销,它都会默认为您提供基于GMT的时间。
作为一个例子,现在对我来说是美国东部时间下午12:44(或者当你读到这个时)。我使用上面的代码编写以下内容:
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [cal components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSMinuteCalendarUnit | NSHourCalendarUnit |NSTimeZoneCalendarUnit) fromDate:[NSDate date]];
comps.timeZone = [NSTimeZone localTimeZone];
NSDate *newDate = [cal dateFromComponents:comps];
NSLog(@"New date %@", newDate);
输出:
New date 2013-11-12 17:44:00 +0000
因此它忽略了我将时区设置为当地时区的事实。但是,如果我把它放在格式化程序中并将其注销,你会注意到时间现在是“正确的”:
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateStyle = NSDateFormatterLongStyle;
formatter.timeStyle = NSDateFormatterLongStyle;
NSLog(@"Date string %@", [formatter stringFromDate:newDate]);
输出:
Date string November 12, 2013 at 12:44:00 PM EST
希望有所帮助。