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