检测时间是否以小时为单位的简单方法是什么?
What is the simple way to detect time is in hours or not?
我用 timeInterval
一分钟在我的项目中使用 UIDatePickerView
。选择时间后,如果时间以小时为单位,那么我必须做一些事情。那么我如何检测用户是否选择了以小时为单位的时间?
Try this one , you can get hours between two date.
NSDate* date1 = someDate;
NSDate* date2 = someOtherDate;
NSTimeInterval distanceBetweenDates = [date1 timeIntervalSinceDate:date2];
double secondsInAnHour = 3600;
NSInteger hoursBetweenDates = distanceBetweenDates / secondsInAnHour;
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc]init]autorelease];
timeFormatter.dateFormat = @"HH";
NSString *date_Hour= [timeFormatter stringFromDate: localDate];
获取小时的简单方法仅使用 NSDateComponents class
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit fromDate:[NSDate date]];
NSInteger hour= [components hour];
我自己的问题终于得到了答案
NSDateFormatter *formatter23 = [[NSDateFormatter alloc]init];
[formatter23 setDateFormat:@"yyyy-MM-dd--HH:mm"];
NSDate* date1 = datePicker.date;
[formatter23 setDateFormat:@"mm"];
if ([[formatter23 stringFromDate:date1] isEqualToString:@"00"] || [[formatter23 stringFromDate:date1] isEqualToString:@"30"]){
NSLog(@"Date is in half an hour or hour");
} else{
NSLog(@"Some other");
}
我用 timeInterval
一分钟在我的项目中使用 UIDatePickerView
。选择时间后,如果时间以小时为单位,那么我必须做一些事情。那么我如何检测用户是否选择了以小时为单位的时间?
Try this one , you can get hours between two date.
NSDate* date1 = someDate;
NSDate* date2 = someOtherDate;
NSTimeInterval distanceBetweenDates = [date1 timeIntervalSinceDate:date2];
double secondsInAnHour = 3600;
NSInteger hoursBetweenDates = distanceBetweenDates / secondsInAnHour;
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc]init]autorelease];
timeFormatter.dateFormat = @"HH";
NSString *date_Hour= [timeFormatter stringFromDate: localDate];
获取小时的简单方法仅使用 NSDateComponents class
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit fromDate:[NSDate date]];
NSInteger hour= [components hour];
我自己的问题终于得到了答案
NSDateFormatter *formatter23 = [[NSDateFormatter alloc]init];
[formatter23 setDateFormat:@"yyyy-MM-dd--HH:mm"];
NSDate* date1 = datePicker.date;
[formatter23 setDateFormat:@"mm"];
if ([[formatter23 stringFromDate:date1] isEqualToString:@"00"] || [[formatter23 stringFromDate:date1] isEqualToString:@"30"]){
NSLog(@"Date is in half an hour or hour");
} else{
NSLog(@"Some other");
}