如何在ios的凌晨4点到4点和下午4点到凌晨4点之间查看时间?

时间:2014-12-16 07:29:52

标签: ios iphone

NSString *time = [self getCurrentDate];
NSLog(@"Current date :%@", time);
NSString *fromDate = @"10:00AM";
NSString *toDate = @"10:00PM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"HH:mma"];
NSDate *fromTime = [dateFormat dateFromString:fromDate];
NSDate *toTime = [dateFormat dateFromString:toDate];
NSDate *nowTime = [dateFormat dateFromString:time];
NSComparisonResult result1 = [nowTime compare:fromTime];
NSComparisonResult result2 = [nowTime compare:toTime];

3 个答案:

答案 0 :(得分:1)

NSDateComponents *calendarComponent = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit
                                                               fromDate:[NSDate date]];
NSInteger hour = [calendarComponent hour];
NSInteger minute = [calendarComponent minute];
NSInteger second = [calendarComponent second];

if (hour >= 4 && hour <= 16) { //16 means 12+4 = 4pm = 16hours
    // 4am to 4pm

    NSLog(@"4am to 4pm");

}
else{
    // 4pm to 4am

    NSLog(@"4pm to 4am");

}

fromDate:应该是你的日期,必须进行测试

答案 1 :(得分:0)

请试试这个:

    NSString *aStrStartDate = @"10:00AM";
    NSString *aStrEndDate = @"10:10PM";

    NSDateFormatter *aDateFormat = [[NSDateFormatter alloc]init];
    [aDateFormat setDateFormat:@"HH:mma"];
    NSDate *aDateStart = [aDateFormat dateFromString:aStrStartDate];
    NSDate *aDateEnd = [aDateFormat dateFromString:aStrEndDate];
    NSTimeInterval aTimeInterval = [aDateEnd timeIntervalSinceDate:aDateStart];
    NSLog(@"aTimeInterval: %f", aTimeInterval);

    NSDate *aDateTimer = [NSDate dateWithTimeIntervalSince1970:aTimeInterval];
    NSLog(@"%@",aDateTimer);
    NSDateFormatter *aDateFormat2 = [[NSDateFormatter alloc] init];
    [aDateFormat2 setDateFormat:@"HH:mm:ss"];
    [aDateFormat2 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];

    NSString *aStrFinalTimeInterval = [aDateFormat2 stringFromDate:aDateTimer];
    NSLog(@"aStrFinalTimeInterval: %@",aStrFinalTimeInterval);

答案 2 :(得分:0)

NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setDateFormat:@"hh:mma"];


NSDate *now = [NSDate date];      // Current Date
NSString *time = [dateFormat stringFromDate:now];
NSString *fromDate = @"10:00AM";
NSString *toDate = @"11:00PM";
NSDate *fromTime = [dateFormat dateFromString:fromDate];
NSDate *toTime = [dateFormat dateFromString:toDate];
NSDate *nowTime = [dateFormat dateFromString:time];
NSDateComponents *fromComponents = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:fromTime];
NSDateComponents *toComponents = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:fromTime];
NSInteger fromHour = [fromComponents hour]; 
NSInteger toHour = [toComponents hour];
if (fromHour >= 4 && toHour <= 16) { 
    NSLog(@"4am to 4pm");
}
else{       
    NSLog(@"4pm to 4am");
}