Search code examples
iosobjective-cnsdatenstimezone

find EST date now in objective-c iOS


Coming to point i need to find the EST time now, but i have written below function this will return incorrect time to me, I didn't point out where i made a mistake or I've written wrong code , any one can drive me in a way would be highly appreciate-able.

@ROB the response will be like that : "close_ts" = "2014-11-21T01:25:00"

 + (NSDate *)currentESTDate
{
    NSString *formatterDate = @"yyyy-MM-dd HH:mm:ss";

    NSDate* date = [NSDate date];
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EST"]];
    [formatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"]];
    [formatter setDateFormat:formatterDate];
    NSString* currentDateStamp = [formatter stringFromDate:date];
    NSDate * returnDate = [formatter dateFromString:currentDateStamp];

    if( returnDate )
    {
        return returnDate;
    }
    return nil;
}

Solution

  • You say the time is reported as 2014-11-21T01:25:00. That's a typical RFC 3339/ISO 8601 date. See Apple's Technical Q&A QA1480 for information on how to parse, which is usually:

    NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.locale = enUSPOSIXLocale;
    formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss";
    formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    NSDate *date = [formatter dateFromString:timeStamp];
    

    Note, in this format, the timestamp is almost always in GMT, not EST. To remove this ambiguity, most dates strings we get from servers are generally bear a Z at the end, e.g. 2014-11-21T01:25:00Z to unambiguously declare that the date in "Zulu", i.e. GMT/UTC. Even in the absence of timezone information, we'd generally expect this to still be GMT. If you know for a fact that it's really EST (which I highly doubt; that would be very unusual), then change the timeZone line above (using your timeZoneWithAbbreviation).

    That's how you convert the date string you received from the server into a NSDate. You can then compare that to [NSDate date] as contemplated in your isEndDate:isSmallerThanCurrent: routine. But do not try to convert a NSDate into a different timezone.


    You say currentESTDate returns the incorrect time. The NSDateFormatter will calculate the current date and time in EST in the currentDateStamp variable. But you then convert it back, just retrieving the original NSDate value. Thus this routine is unnecessary.

    To be clear, NSDate objects do not have timezones. They represent the same moment of time across the entire world. The time zones only come into play when you convert the date into a string (via NSDateFormatter).

    Thus, the problem is not "how do I convert [NSDate date] into a particular timezone." The question is how one converts the 2014-11-21T01:25:00 string from the server into a valid NSDate object (which can then be compared to other NSDate objects). And hopefully the above code answers that question.