与时间一起工作没有给出正确的结果 iphone?
我想要结束时间和当前时间之间的最大时间。我尝试了这个,但没有给出正确的结果。
NSString *strFreq = @"2";
NSString *strET = @"5:20 PM"
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]autorelease];
NSDateComponents *dateComponentsToday = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit) fromDate:today];
int currentHour = [dateComponentsToday hour];
int currentMinute = [dateComponentsToday minute];
int freqAddedMinute = currentMinute + [strFreq intValue];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"h:mm a"];
NSDate *date = [formatter dateFromString:strET];
NSCalendar *gregorian1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian1 components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:date];
NSLog(@"currentHour %d",currentHour);
NSLog(@"freqAddedMinute %d",freqAddedMinute);
NSLog(@"endTimeHour %d",[components hour]);
NSLog(@"endTimeMinute %d",[components minute]);
int endTimeHour = [dateComponentsToday hour];
int endTimeMinute = [dateComponentsToday minute];
// Suppose my current time is 5:10 PM then this condition must be false but it is getting true.
if (currentHour == endTimeHour && freqAddedMinute > endTimeMinute) {
NSLog(@"reach end time");
}
请帮助我。提前致谢。
I want maximum time from end time and current time. I tried this but not giving proper result.
NSString *strFreq = @"2";
NSString *strET = @"5:20 PM"
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]autorelease];
NSDateComponents *dateComponentsToday = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit) fromDate:today];
int currentHour = [dateComponentsToday hour];
int currentMinute = [dateComponentsToday minute];
int freqAddedMinute = currentMinute + [strFreq intValue];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"h:mm a"];
NSDate *date = [formatter dateFromString:strET];
NSCalendar *gregorian1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian1 components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:date];
NSLog(@"currentHour %d",currentHour);
NSLog(@"freqAddedMinute %d",freqAddedMinute);
NSLog(@"endTimeHour %d",[components hour]);
NSLog(@"endTimeMinute %d",[components minute]);
int endTimeHour = [dateComponentsToday hour];
int endTimeMinute = [dateComponentsToday minute];
// Suppose my current time is 5:10 PM then this condition must be false but it is getting true.
if (currentHour == endTimeHour && freqAddedMinute > endTimeMinute) {
NSLog(@"reach end time");
}
Please help me for this. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这个
http://developer.apple.com/library/IOS/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html
Check this out
http://developer.apple.com/library/IOS/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html