将当前时间与固定时间 05:00:00 PM 进行比较

发布于 2024-11-05 13:37:52 字数 97 浏览 0 评论 0原文

我如何将当前时间 [NSDate date] 与固定时间 05:00:00 PM 进行比较。

那个 05:00 PM 是否已经过去了。我只需要 BOOL 检查这个。

How will I compare current time [NSDate date] with fixed time 05:00:00 PM.

That 05:00 PM is already passed or not. I just need BOOL check for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哆啦不做梦 2024-11-12 13:37:52
- (BOOL)past5pm
{
    NSCalendar *gregorianCalender = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDateComponents *components = [gregorianCalender components:NSHourCalendarUnit fromDate:[NSDate date]];

    if([components hour] >= 17) // NSDateComponents uses the 24 hours format in which 17 is 5pm
       return YES;

    return NO;
}
- (BOOL)past5pm
{
    NSCalendar *gregorianCalender = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDateComponents *components = [gregorianCalender components:NSHourCalendarUnit fromDate:[NSDate date]];

    if([components hour] >= 17) // NSDateComponents uses the 24 hours format in which 17 is 5pm
       return YES;

    return NO;
}
锦爱 2024-11-12 13:37:52
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init]  autorelease];
[dateFormatter setDateFormat:@"HH.mm"];
NSDate *currentDate = [NSDate date];
NSString *curDate = [dateFormatter stringFromDate:currentDate];

if ([curDate doubleValue] >= 17.00) 
{
    //set your bool
}
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init]  autorelease];
[dateFormatter setDateFormat:@"HH.mm"];
NSDate *currentDate = [NSDate date];
NSString *curDate = [dateFormatter stringFromDate:currentDate];

if ([curDate doubleValue] >= 17.00) 
{
    //set your bool
}
丑疤怪 2024-11-12 13:37:52

试试这个

NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
        [dateFormatter setDateFormat:@"hh:mm:ss a"];

        NSDate* date = [NSDate date];

        //Get the string date
        NSString* str = [dateFormatter stringFromDate:date];

        NSDate *firstDate = [dateFormatter dateFromString:@"05:00:00 PM"];

        NSDate *secondDate = [dateFormatter dateFromString:str];

        NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];

        NSLog(@"Time Diff - %f",timeDifference);

Try this

NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
        [dateFormatter setDateFormat:@"hh:mm:ss a"];

        NSDate* date = [NSDate date];

        //Get the string date
        NSString* str = [dateFormatter stringFromDate:date];

        NSDate *firstDate = [dateFormatter dateFromString:@"05:00:00 PM"];

        NSDate *secondDate = [dateFormatter dateFromString:str];

        NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];

        NSLog(@"Time Diff - %f",timeDifference);
帅气尐潴 2024-11-12 13:37:52

您可以使用纯 C 风格代码并以整数形式获取差异,然后根据差异是正数还是负数决定需要从比较函数返回什么。您也可以通过这种方式比较分钟数。不要忘记导入 time.h。

    time_t now = time(NULL);
    struct tm oldCTime;
    localtime_r(&now, &oldCTime);
    int hours = oldCTime.tm_hour;
    int diff = 17-hours;
    NSLog(@"Time difference is: %d.", diff);

You can probably use plain C style code and get the difference as an integer and then decide what you need to return from the comparison function depending on wether the difference is positive or negative. You can compare minutes this way as well. Dont forget to import time.h.

    time_t now = time(NULL);
    struct tm oldCTime;
    localtime_r(&now, &oldCTime);
    int hours = oldCTime.tm_hour;
    int diff = 17-hours;
    NSLog(@"Time difference is: %d.", diff);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文