0

My Problem, in my app i used to show event for each week (week starts with monday), on next button i will be calculating next week dates, my working was working fine , but for a specific day it not returning proper value ,

following is my code

with current week end date , i will add one day to it to get next week start date

 NSDateComponents *dateComponet = [[NSDateComponents alloc] init];
    [dateComponet setDay:days];
    return [[self getCustomCalendar] dateByAddingComponents:dateComponet toDate:date   options:0];

where [self getCustomeCalendar] is,

  NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [gregorian setLocale:[NSLocale currentLocale]];
    [gregorian setFirstWeekday:2]; // 2 monday
    return gregorian;

following are the return value for some weeks,

weekend Date: 2014-02-23 00:00:00 +0000 , next weekStartDate : 2014-02-24 00:00:00 +0000 weekend Date: 2014-03-02 00:00:00 +0000 , next weekStartDate : 2014-03-03 00:00:00 +0000 weekend Date: 2014-03-09 00:00:00 +0000 , next weekStartDate : 2014-03-10 00:00:00 +0000 weekend Date: 2014-03-16 00:00:00 +0000 , next weekStartDate : 2014-03-17 00:00:00 +0000
weekend Date: 2014-03-23 00:00:00 +0000 , next weekStartDate : 2014-03-24 00:00:00 +0000

// this is the problem one

weekend Date: 2014-03-30 00:00:00 +0000 , next weekStartDate : 2014-03-30 23:00:00 +0000

as you can see all dates are returning properly by adding one day,

but for 2014-03-30 , when +1 day is added, it should return 2014-03-31 instead, it is returned with 23 hours, i couldn't able to find the cause for this issue

any idea why this is happening ?

Ramesh Lingappa
  • 2,448
  • 20
  • 33
  • 1
    It looks like daylight saving is the issue. – Martin Koles Feb 13 '14 at 12:09
  • ya initially the problem occurred with march 9 (i.e) march 9 + one day returns => march 9 23 hours, any suggested solution to resolve this issue ? – Ramesh Lingappa Feb 13 '14 at 12:12
  • I had this issue and resolved it in my case that I strictly worked with all dates object in GMT time zone. This worked for me, as I only needed to work with unified dated. I remember Apple had a talk on this on one of wwdc 2013 sessions. – Martin Koles Feb 13 '14 at 12:16
  • yes initially i too did the same, i use to find the week start date by setting timezone to GMT, so following dates will be calculated appropriately, but that bring the usability issue as customer see different dates from local date for timezone difference greater than 12 hours which leads to next day for some times – Ramesh Lingappa Feb 13 '14 at 12:22
  • note: i tried creating with GMT timezone like, [calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; but this solve the issue only for some timzone like London, but for NewYork the problem dint get resolved – Ramesh Lingappa Feb 13 '14 at 12:26
  • Checkout this: http://stackoverflow.com/questions/17634715/is-this-method-going-to-always-give-me-120000-of-the-current-day-in-ios There's a link to the wwdc session as well. – Martin Koles Feb 13 '14 at 12:31
  • thank you, will have a look! – Ramesh Lingappa Feb 13 '14 at 12:34

1 Answers1

0

Okay even though i couldn't resolve the daylight saving issue, i was able to solve my problem by calculation like below,

Usually i will take the calculated value as week start date, but now what am doing is i will calculate the week start date (current week endDate + 1 ), then will do another calculation to find out what the actual start date for that particular week using NSCalendar (with GMT) which resolves this problem, thanks for the comments and suggestions,

if someone finds better solution, please post it.

Ramesh Lingappa
  • 2,448
  • 20
  • 33