0

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;
}
Arun
  • 3,406
  • 4
  • 30
  • 55
  • What's the point of converting the current date to a string and then converting that string back to a date and returning that date? Why not just use `[NSDate date]` as-is? – rmaddy Nov 06 '14 at 05:25
  • @rmaddy i need est format current date, but NSString* currentDateStamp = [formatter stringFromDate:date]; this will return exact time in string format, i need that in date formate so i convert that to again [formatter dateFromString:currentDateStamp] this will give a false time . – Arun Nov 06 '14 at 05:30
  • 2
    See Rob's answer. It seems you have some confusion of how dates work. – rmaddy Nov 06 '14 at 05:33
  • If you search S.O. for questions about getting `NSDate` in particular timezone, perhaps that will provide useful background. E.g. see http://stackoverflow.com/questions/7485493/get-nsdate-from-nsdate-adjusted-with-timezone – Rob Nov 06 '14 at 05:35
  • @rmaddy pls look at Once i have updated few lines of code and i need to pass the values – Arun Nov 06 '14 at 05:41
  • please mention the reason for downwote ? – Arun Nov 06 '14 at 06:41
  • 1
    I didn't down-vote, but I'd guess it was down-voted because this has been asked many times. It's a common stumbling block when people first start using `NSDate`. No offense intended, but it's not clear how much research you did looking for similar questions before asking it again, yourself. I personally cut you a little slack because the connection between the original question "how do I convert timezone of `NSDate` object" and the root problem (failure to convert `2014-11-21T01:25:00` properly) is not entirely obvious. But both topics have been covered exhaustively here before. :) – Rob Nov 06 '14 at 07:22

1 Answers1

2

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.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • actually my primary concern is to compare 2 dates one is service returns the EST date and i need to convert my date to current EST date, then how it works last one fullday i analysed and searched all the possible things but i didn't got idea. – Arun Nov 06 '14 at 05:37
  • my question is dublicate one ?…. pls suggest me – Arun Nov 06 '14 at 06:47
  • 1
    @Spynet Yeah, it's a duplicate, but I cut you a little slack as this is one of those questions that it's hard to find good answers (unless you already know the answer). lol. Regarding parsing `2014-11-21T01:25:00` format, search for "[ios] nsdate t" (though, many of the answers out there neglect the subtlety of setting the `locale`, too). Regarding original `NSDate`/timezone question, search for "[ios] nsdate current time zone", and you'll find some promising links (this misconception about `NSDate` and timezones is pretty common, as you'll see, so don't feel bad). – Rob Nov 06 '14 at 07:10