ios – 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];
}

解决方法

我写了一个小类来查找两个NSDate对象之间的天数.如果我理解你的问题,那就是你想要的.

NSDate DaysDifference.h

#import <Foundation/Foundation.h>

@interface NSDate (DaysDifference)

- (NSInteger)differenceInDaysToDate:(NSDate *)otherDate;

@end

NSDate DaysDifference.m

#import "NSDate+DaysDifference.h"

@implementation NSDate (DaysDifference)

- (NSInteger)differenceInDaysToDate:(NSDate *)otherDate {
    NSCalendar *cal = [NSCalendar autoupdatingCurrentCalendar];
    NSUInteger unit = NSDayCalendarUnit;
    NSDate *startDays,*endDays;

    [cal rangeOfUnit:unit startDate:&startDays interval:NULL forDate:self];
    [cal rangeOfUnit:unit startDate:&endDays interval:NULL forDate:otherDate];

    NSDateComponents *comp = [cal components:unit fromDate:startDays toDate:endDays options:0];
    return [comp day];
}

@end

相关文章

背景 前端时间产品经理决定使用百度统计,使得 工程B 中原统计sdk-友盟统计,需要被去除。之前尝试去除...
结论: alloc负责分配内存和创建对象对应的isa指针; init只是返回alloc生成的对象。 所以alloc后,多次...
更新 如果UI愿意把启动图切割成n份,按一定约束在launchscreen.storyboard中进行排版,启动图效果会更好...
最近在看一本书《Effective OC 2.0》,今天看到有个tip是OC适中循环各自优劣性,作者最终推荐此块循环。...
// // ViewController.m // paintCodeTestOC //gif // Created by LongMa on 2019/7/25. // #import &a...
背景介绍 一般情况下,出于省电、权限、合理性等因素考虑,给人的感觉是很多奇怪的需求安卓可以实现,但...