NSDate - 抵消时区

Eri*_*ric 7 objective-c nsdate

我一定是想念她的小事,但无法理解.试图创建一个比较日期,但我似乎无法将currentDate从GMT抵消到EST:

// current date (gmt) //
NSDate *currentDate = [NSDate date];

NSTimeZone *currentDateTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"];

NSDateFormatter *currentDateFormat = [[NSDateFormatter alloc]init];
[currentDateFormat setTimeZone:currentDateTimeZone];
[currentDateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];

NSString *currentDateString = [currentDateFormat stringFromDate:currentDate];
NSLog(@"currentDateString: %@", currentDateString); // returns 2011-01-05 13:30:30 EST

NSDate *currentDateWithOffset = [currentDateFormat dateFromString:currentDateString];

NSLog(@"currentDateWithOffset: %@", currentDateWithOffset); // returns 2011-01-05 18:30:30 +0000
Run Code Online (Sandbox Code Playgroud)

谢谢!

编辑:

我正在使用以下行在一个单独的类中调用一个方法(尝试使其成为可移植的):

[Expiration expires:[[NSDate alloc] initWithString:@"2011-01-07 12:00:00 +0000"] within:1.0]
Run Code Online (Sandbox Code Playgroud)

在expires方法中,我有以下几行:

NSComparisonResult comparison = [currentDateWithOffset compare:expires]; // check for a fixed date to disable the demo

double withinRange = [installDate timeIntervalSinceDate:currentDateWithOffset]; // check for number of seconds between "within" and the install date
Run Code Online (Sandbox Code Playgroud)

我然后比较这两个值:

if(withinRange >= within && withinRange > 0.0) {
    // app is expired //
}
else {
    // app is still enabled (so far...) //
    if(comparison == NSOrderedDescending || comparison == NSOrderedSame) {
        // app is expired //
    }
    else {
        // app is still enabled //
    }
}
Run Code Online (Sandbox Code Playgroud)

这有帮助吗?谢谢你的耐心!

编辑:

这是整个过期:在目前的方法中......

+(BOOL)expire:(NSDate*)expires within:(double)within {
   // default expired value //
    BOOL expired = NO;

    // convert within value from days to seconds //
    within *= 24.0 * 60.0 * 60.0;

    // current date (gmt) //
    NSDate *currentDate = [NSDate date];

    // install date //
    NSDate *installDate = [[NSUserDefaults standardUserDefaults]objectForKey:@"installDate"];

    // check for a value in installDate //
    if (nil == installDate) {
        // app is running for the first time //
        [[NSUserDefaults standardUserDefaults]setObject:currentDate forKey:@"installDate"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        installDate = currentDate;
    }

    if([installDate timeIntervalSinceNow] < (-within)) {
        expired = YES;
    }
    else {
        if([expires timeIntervalSinceNow] < 0) {
            expired = YES;
        }
    }

    NSLog(@"installDate:%@", installDate);
    NSLog(@"expires:%@", expires);
    NSLog(@"currentDate:%@", currentDate);

    return expired; 
}
Run Code Online (Sandbox Code Playgroud)

然后我从另一个班级调用它

message.text = (YES == [Expiration expire:[[NSDate alloc] initWithString:@"2011-01-07 12:00:00 -0500"] within:(0.015625/2)]) ? @"This App is Expired" : @"This App is Active";
Run Code Online (Sandbox Code Playgroud)

在模拟器(新安装的应用程序)中运行时,NSLog显示了这个......

[Session started at 2011-01-06 10:43:46 -0500.]
2011-01-06 10:43:48.146 TimeBasedDemo[14717:207] installDate:2011-01-06 15:43:48 +0000
2011-01-06 10:43:48.147 TimeBasedDemo[14717:207] expires:2011-01-07 17:00:00 +0000
2011-01-06 10:43:48.147 TimeBasedDemo[14717:207] currentDate:2011-01-06 15:43:48 +0000
Run Code Online (Sandbox Code Playgroud)

sam*_*tte 17

这些答案都没有给我一个NSDate当前的本地日期的对象.所以这就是我做的方式:

NSDate *currentDateWithOffset = [NSDate dateWithTimeIntervalSinceNow:[[NSTimeZone localTimeZone] secondsFromGMT]];
Run Code Online (Sandbox Code Playgroud)


Nic*_*ore 5

一个NSDate对象表示的时间瞬间,不论时区和日历因素.打印或解析日期时,时区信息是相关的,但它不存储在日期NSDate.

假设您正在创建这样的到期日期:

NSDate *exp=[[NSDate alloc] initWithString:@"2011-01-07 12:00:00 +0000"]
Run Code Online (Sandbox Code Playgroud)

这表示你希望到期时间在格林尼治标准时间中午1点,如果你希望它在美国东部时间中午到期,请用-0500创建它.你应该不会需要做的就是惹当你做一个比较,当前时间.

这是一个简单的方法,看看时间是否过去了

if ([exp timeIntervalSinceNow]<0) { /* it's expired */ }
Run Code Online (Sandbox Code Playgroud)

并且您可以看到within自安装日期以来是否已经过了秒,如下所示:

if ([installDate timeIntervalSinceNow]<(-within)]) { /* it's expired */}
Run Code Online (Sandbox Code Playgroud)