0
votes

I have some Date Strings on server, which I want to parse in my app, get dates and display it on calendar which my app has and not the device calendar.

To do this I am doing it this way

    _dateFromatter = [[NSDateFormatter alloc] init];
    [_dateFromatter setDateFormat:@"dd-MMM-yyyy"];
    [_dateFromatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]]; 
    [_dateFromatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];   


    _gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    [_gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; 


    NSDate *historyDate  = [_dateFromatter dateFromString:[data objectForKey:@"dateStringFromServer"]];

    if(historyDate != nil)
    {
         NSDateComponents *weekdayComponents = [_gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit)fromDate:historyDate];

         NSInteger year  = [weekdayComponents year];
         NSInteger month = [weekdayComponents month];
         NSInteger day   = [weekdayComponents day];

         NSDateComponents *timeZoneComps=[[NSDateComponents alloc] init];
         [timeZoneComps setYear:year];
         [timeZoneComps setMonth:month];
         [timeZoneComps setDay:day];
         [timeZoneComps setHour:00];
         [timeZoneComps setMinute:00];
         [timeZoneComps setSecond:01];

         NSDate *convertedHistoryDate =[_gregorian dateFromComponents:timeZoneComps];
    }

I am adding a break point and making some checks when the execution stops at a break point.

Now here, on server the string is 8-Jul-2014, and If I mouse hover the date I see 2014-07-08 05:30:00 IST, and if I click on info I get 2014-07-08 00:00:00 +0000,

Secondly Now If I change the timezone for NSCalendar and dateformatter to localtime zone and If I check get this 2014-07-08 00:00:00 IST and inside info I get 2014-07-07 18:30:00 +0000

So I am not understanding these two cases. Also I want to know whenever I get dates from server , does time zone matters and what time zone it should be?

Because my users can be in any timezone? so when they sync data they should get whatever dates are present on server. I mean if 8-Jul-2014 is present on server then, if user in US is syncing it should not make it 7-Jul -2014 it should remain 8 -Jul -2014 only.

Regards Ranjit

1
What do you mean by "mouse hover the date"? Using what interface? - trojanfoe
I am using xcode, so If I add a break point and when the code runs and stops at the break point, I can mouse hover the object right - Ranjit
OK... so please update your question to make that clear to others. - trojanfoe
Hey I have updated it - Ranjit
Hey @trojanfoe, please help - Ranjit

1 Answers

0
votes

Regarding Dates, they are actually same represented by different timezone..

2014-07-08 05:30:00 IST/+05:30
2014-07-08 00:00:00 +0000

2014-07-08 00:00:00 IST/+05:30 
2014-07-07 18:30:00 +0000

You need to set the formatter timezone correct while converting date to string and visa versa...

[inputDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];//server timezone
[outputDateFormatter setTimeZone:[NSTimeZone systemTimeZone]];//user's timezone