iOS - 如何比较两个 ISO 8601 格式的时间?

发布于 2024-12-21 17:56:57 字数 64 浏览 2 评论 0原文

我想检查“2011-03-29T15:57:02.680-04:00”之类的时间是否在当前时间之前。我该怎么做呢?

I want to check if a time such as "2011-03-29T15:57:02.680-04:00" is before the current time. How would I go about doing this?

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

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

发布评论

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

评论(2

我乃一代侩神 2024-12-28 17:56:57

ISO8601 日期和时间格式的优点在于您可以简单地按字母顺序比较字符串。因此,您可以将当前时间写入 ISO8601 格式的 NSString,然后对两个字符串使用 NSString 的 compare 方法。

但是,通常最好比较 NSDate 对象。我使用两个辅助函数来使用 strftimestrptime 在 ISO 日期字符串和 NSDate 之间进行转换 - 这些函数只执行 yyyy-mm-dd code> 部分,但您应该能够足够轻松地扩展它们:

NSString* ISOStringWithDate(NSDate* date)
{
    char buf[11];  // Enough space for "yyyy-mm-dd\000"
    time_t clock = [date timeIntervalSince1970];
    struct tm time;
    gmtime_r(&clock, &time);
    strftime_l(buf, sizeof(buf), "%Y-%m-%d", &time, NULL);
    return [NSString stringWithUTF8String:buf];
}

NSDate* dateWithISOString(NSString* dateString)
{
    struct tm time;
    memset(&time, 0, sizeof(time));
    if (!strptime_l([dateString UTF8String], "%Y-%m-%d", &time, NULL))
    {
        return nil;
    }
    time_t clock = timegm(&time);
    return [NSDate dateWithTimeIntervalSince1970:clock];
}

The great thing about ISO8601 date and time formats is that you can simply compare the strings alphabetically. So you can write the current time to an NSString in ISO8601 format and then use NSString's compare method on the two strings.

However, it's often better to compare NSDate objects. I use two helper functions to convert between an ISO date string and an NSDate using strftime and strptime -- these functions just do the yyyy-mm-dd part, but you should be able to extend them easily enough:

NSString* ISOStringWithDate(NSDate* date)
{
    char buf[11];  // Enough space for "yyyy-mm-dd\000"
    time_t clock = [date timeIntervalSince1970];
    struct tm time;
    gmtime_r(&clock, &time);
    strftime_l(buf, sizeof(buf), "%Y-%m-%d", &time, NULL);
    return [NSString stringWithUTF8String:buf];
}

NSDate* dateWithISOString(NSString* dateString)
{
    struct tm time;
    memset(&time, 0, sizeof(time));
    if (!strptime_l([dateString UTF8String], "%Y-%m-%d", &time, NULL))
    {
        return nil;
    }
    time_t clock = timegm(&time);
    return [NSDate dateWithTimeIntervalSince1970:clock];
}
无边思念无边月 2024-12-28 17:56:57

使用 Peter Hosey 的 ISO8601DateFormatter 类将其解析为 NSDate 对象,然后将其与 [NSDate date] 进行比较。

一个例子:

NSString *iso8601String = ...;
ISO8601DateFormatter *formatter = [[ISO8601DateFormatter alloc] init];
NSDate *isoDate = [formatter dateFromString:iso8601String];
[formatter release]; //if you're not using ARC

BOOL isBeforeCurrent = [[NSDate date] compare:isoDate] == NSOrderedAscending;

Use Peter Hosey's ISO8601DateFormatter class to parse it into an NSDate object and then compare that with [NSDate date].

An example:

NSString *iso8601String = ...;
ISO8601DateFormatter *formatter = [[ISO8601DateFormatter alloc] init];
NSDate *isoDate = [formatter dateFromString:iso8601String];
[formatter release]; //if you're not using ARC

BOOL isBeforeCurrent = [[NSDate date] compare:isoDate] == NSOrderedAscending;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文