ios - NSDate 比较,找出特定(本地)时区的 "number of midnights between"

标签 ios cocoa-touch cocoa nsdate

我是不是漏掉了什么? Apple 提供的方法似乎只适用于 UTC,无论机器的时区默认值如何,或者您将其设置为什么。

这是我得到的输出:

Output:
2013-02-01 10:41:24.152 Scratch[17640:c07] cal=gregorian, cal.timeZone=America/Los_Angeles (PST) offset -28800
2013-02-01 10:41:24.154 Scratch[17640:c07] date_Feb1_1400PST=2013-02-01 14:00 -0800
2013-02-01 10:41:24.156 Scratch[17640:c07] date_Feb2_1200PST=2013-02-02 12:00 -0800
2013-02-01 10:41:24.157 Scratch[17640:c07] midnights between=1
2013-02-01 10:41:24.158 Scratch[17640:c07] and then...
2013-02-01 10:41:24.159 Scratch[17640:c07] date_Feb1_2000PST=2013-02-01 22:00 -0800
2013-02-01 10:41:24.161 Scratch[17640:c07] date_Feb2_1000PST=2013-02-02 10:00 -0800
2013-02-01 10:41:24.161 Scratch[17640:c07] midnights between=0

我真正想知道的是给定时区(本地或其他时区,不一定是 UTC )

这似乎是一个如此常见且相当简单的问题,但我很惊讶地发现它是如此困惑和难以弄清楚。

我不是在寻找涉及“mod 86400”或类似肮脏内容的答案。该框架应该能够认真地告诉我这一点。

- (void)doDateComparisonStuff {
    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    cal.timeZone = [NSTimeZone timeZoneWithName:@"America/Los_Angeles"];
    NSLog(@"cal=%@, cal.timeZone=%@", cal.calendarIdentifier, cal.timeZone);

    NSDate *date_Feb1_1400PST = [self dateFromStr:@"20130201 1400"];
    NSLog(@"date_Feb1_1400PST=%@", [self stringFromDate:date_Feb1_1400PST]);

    NSDate *date_Feb2_1200PST = [self dateFromStr:@"20130202 1200"];
    NSLog(@"date_Feb2_1200PST=%@", [self stringFromDate:date_Feb2_1200PST]);

    NSLog(@"midnights between=%d", [self daysWithinEraFromDate:date_Feb1_1400PST toDate:date_Feb2_1200PST usingCalendar:cal]);

    NSLog(@"and then...");

    NSDate *date_Feb1_2000PST = [self dateFromStr:@"20130201 2200"];
    NSLog(@"date_Feb1_2000PST=%@", [self stringFromDate:date_Feb1_2000PST]);

    NSDate *date_Feb2_1000PST = [self dateFromStr:@"20130202 1000"];
    NSLog(@"date_Feb2_1000PST=%@", [self stringFromDate:date_Feb2_1000PST]);

    NSLog(@"midnights between=%d", [self daysWithinEraFromDate:date_Feb1_2000PST toDate:date_Feb2_1000PST usingCalendar:cal]);
}

// based on "Listing 13" at
// https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html#//apple_ref/doc/uid/TP40007836-SW1
- (NSInteger)daysWithinEraFromDate:(NSDate *)startDate toDate:(NSDate *)endDate usingCalendar:(NSCalendar *)cal
{
    NSInteger startDay=[cal ordinalityOfUnit:NSDayCalendarUnit
                                       inUnit: NSEraCalendarUnit forDate:startDate];
    NSInteger endDay=[cal ordinalityOfUnit:NSDayCalendarUnit
                                     inUnit: NSEraCalendarUnit forDate:endDate];
    return endDay-startDay;
}


- (NSDate *)dateFromStr:(NSString *)dateStr {
    NSDateFormatter *df = nil;
    df = [[NSDateFormatter alloc] init];
    df.timeZone = [NSTimeZone timeZoneWithName:@"America/Los_Angeles"];
    df.dateFormat = @"yyyyMMdd HHmm";

    return [df dateFromString:dateStr];
}

- (NSString *)stringFromDate:(NSDate *)date {
    NSDateFormatter *df = nil;
    df = [[NSDateFormatter alloc] init];
    df.timeZone = [NSTimeZone timeZoneWithName:@"America/Los_Angeles"];  // native timezone here
    df.dateFormat = @"yyyy-MM-dd HH:mm Z";

    return [df stringFromDate:date];
}

最佳答案

使用 this answer作为起点,我只是将日历添加为附加参数(如果需要,您可以只传递时区而不是日历):

- (NSInteger)daysBetweenDate:(NSDate*)fromDateTime andDate:(NSDate*)toDateTime usingCalendar:(NSCalendar *)calendar
{
    NSDate *fromDate;
    NSDate *toDate;

    [calendar rangeOfUnit:NSDayCalendarUnit startDate:&fromDate
                 interval:NULL forDate:fromDateTime];
    [calendar rangeOfUnit:NSDayCalendarUnit startDate:&toDate
                 interval:NULL forDate:toDateTime];

    NSDateComponents *difference = [calendar components:NSDayCalendarUnit
                                               fromDate:fromDate toDate:toDate options:0];

    return [difference day];
}

这将使用您传递的日历来确定 fromDateTimetoDateTime 之间的午夜数,并将其作为 NSInteger 返回。确保正确设置时区。

使用上面的示例,这是输出:

2013-03-07 17:23:54.619 Testing App[69968:11f03] cal=gregorian, cal.timeZone=America/Los_Angeles (PST) offset -28800
2013-03-07 17:23:54.621 Testing App[69968:11f03] date_Feb1_1400PST=20130201 1400
2013-03-07 17:23:54.621 Testing App[69968:11f03] date_Feb2_1200PST=20130202 1200
2013-03-07 17:23:54.622 Testing App[69968:11f03] midnights between=1
2013-03-07 17:23:54.622 Testing App[69968:11f03] and then...
2013-03-07 17:23:54.623 Testing App[69968:11f03] date_Feb1_2000PST=20130201 2200
2013-03-07 17:23:54.624 Testing App[69968:11f03] date_Feb2_1000PST=20130202 1000
2013-03-07 17:23:54.624 Testing App[69968:11f03] midnights between=1

所以是的,我支持我的投票,将这个问题作为重复问题关闭,因为它非常接近您正在做的事情。 :-)

关于ios - NSDate 比较,找出特定(本地)时区的 "number of midnights between",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14653114/

相关文章:

objective-c - 将数组的每个对象与其他每个对象进行比较

ios - 带 SSL 固定的 AFHTTPRequestOperation 不起作用

iphone - 提醒应用程序 - 重复某个提醒

objective-c - 你如何在 Objective C 中使用 "abort"方法?

objective-c - 如何在 Objective-C 中将句子的第一个单词大写?

macos - 使用 NSPredicate 按关键字过滤 NSArray

iphone - Objective C - 实现和扩展之间的区别

ios - Xcode 11 Beta 5 未显示 Swift 5.1

ios - 当我们在 swift iOS 的 UIWebView 上有一些内容时,如何获得正确的高度?

objective-c - 如何在 XCode 中使用调试器检查变量的值