-1

I have a calendar app, in which I highlight current date,

I have followed the following steps, at present my device time is 11:59 am,

Indian time zone, I change it to first 9.am, then I change time zone to san jose ,

I get time as 25th june 9.34 pm, now in device calendar it shows 25th june, but

in my app it still shows 26th june.

I am getting current date this way

-(void)initialize
{
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit  | NSMinuteCalendarUnit)fromDate:[NSDate date]];
    NSInteger day    = [weekdayComponents day];
    NSInteger month  = [weekdayComponents month]; 
    NSInteger year   = [weekdayComponents year];

   [m_dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

    m_dateFormatter.dateFormat = @"dd/MM/yyyy";

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

    m_currentDate         = [gregorian dateFromComponents:timeZoneComps];   

}

So, I need your help in this regard.

Ranjit
  • 4,576
  • 11
  • 62
  • 121
  • Because you are explicitly using `UTC` time zone? – trojanfoe Jun 26 '14 at 06:53
  • I removed it , now for san jose it works fine and in my app, it shows 25th june, but now if I change time zone to australia which is ahead of indian time zone, it should show me 26th june, but it shows 25th june. Why can that problem be due to? – Ranjit Jun 26 '14 at 07:13
  • Hello @trojanfoe, can you please have a look at my comment – Ranjit Jun 30 '14 at 13:15

1 Answers1

0

I think that [NSTimeZone systemTimeZone] will always give you the current timezone as detected (or set) by the device. Since the timezone is cached in your app you can also call [NSTimeZone resetSystemTimeZone] to make sure that the info is up to date. So, in your code:

[NSTimeZone resetSystemTimeZone]; [m_dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];

spassas
  • 4,778
  • 2
  • 31
  • 39
  • Hey @spassas as I have created my calendar by setting timezone to UTC, I cant change that now, bcoz it will break few things, is there something we can do, keeping time zone as UTC. – Ranjit Jun 30 '14 at 09:34
  • Umm, I'm not sure I follow. You are trying to get the current date in the current timezone, is that correct? So you cannot explicitly use `UTC` timezone... If I may ask, what is it you're afraid of breaking? – spassas Jun 30 '14 at 13:42
  • I have used UTC time zone for my previous versions of the app. So now if I use system time zone then I will get different time. so now the previous dates will be in UTC and the new dates will be in systemtime zone so the time differs – Ranjit Jun 30 '14 at 13:45
  • I don't see how that would cause problems. Just retrieve the current timezone, get the day/month in the timezone and highlight the appropriate cell in your app. Why would that break any previous versions? – spassas Jun 30 '14 at 14:09
  • I will try it out and get back to you. – Ranjit Jun 30 '14 at 15:02
  • Hello @spassas, I will explain my problem clearly, user can select dates from calendar and I am saving it in plist , Now my current calendar is made from UTC, so whenever I select a date I assign to it time 00:00:01 so when I save it to plist it will show me for example 2-jul-2014, 5:30:01, So all dates are in same way. Now suddenly if I change to current time zone, now the new dates which user will save will not have 5:30:01 as time stamp and will save the current time, so when I retrieve whether their will be issues? – Ranjit Jul 07 '14 at 06:33