【问题标题】:How to check time in between 4am to 4pm and 4pm to 4am in ios? [closed]如何在ios中查看凌晨4点到下午4点和下午4点到凌晨4点之间的时间? [关闭]
【发布时间】:2014-12-16 07:29:51
【问题描述】:
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];

【问题讨论】:

  • 传统的做法是在问题中包含一些实际文本,以便提出问题。我读过标题,但我不知道你在问什么。

标签: ios iphone


【解决方案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: 应该是你需要测试的日期

【讨论】:

    【解决方案2】:
    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");
    }
    

    【讨论】:

      【解决方案3】:

      请试试这个:

          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);
      

      【讨论】:

        猜你喜欢
        • 2021-04-13
        • 2023-03-24
        • 1970-01-01
        • 2022-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-02
        相关资源
        最近更新 更多