Sunday, 2 November 2014

How to calculate number of days between current date and selected weekday in IOS SDK




Mostly in IOS app development we need alarm functionality in our apps. As we all know that we can fire alarm in our IOS app as UILocalNotification. But to fire alarm or UILocalNotification we need  seconds or time to be passed when scheduling our UILocalNotification. If you did not know much about UILocalNotification, then you can read  about UILocalNotification via visiting below given link

UILocalNotification Tutorial

Recently, while developing an app for my client i stuck in problem where i need to figure out the date so that i can set time for Alarm to be fired at that particular date selected by user. Now, here one can say that its very easy to get number of days between to dates in IOS SDK or in objective C or in Xcode. But in my app user will select day and then time for the alarm to be fired. Now at this point i had only one date that is current date or you can say today's date and a weekday as selected by user.
SO in this post i will post the solution adopted by me so that anyone stuck in this kind of situation can use this solution.

Using NSCalendar class we can find out weekday from current date.

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]];

    int weekday = [comps weekday];

We will assign integer to our weekdays button too.

Now we had weekday of current date and weekday selected by user, with these two things we need to calculate next or second date.

We will store current date in currentDate variable.

    NSDate *currentDate = [NSDate date];

 Here we have two conditions either day selected by user is the same day or  next week day. If its same day then we can use current date as next date and calculate time between two dates. So here i am going to show you solution for second scenario in which days are not same. We will take daysToadd as a variable and assign zero to it means there is no need to add days while calculating next date.

int daysToAdd = 0;

Next, if current weekday is smaller then mySelectedweekday(week day selected by user) then daysToAdd will be equal to mySelectedweekday - weekday.

Otherwise, daysToAdd will be equals to mySelectedweekday - weekday+7;

if (weekday
{
    daysToAdd = mySelectedweekday-weekday;
}
else
{
   daysToAdd = mySelectedweekday-weekday+7;
}

Finally we will get newDate by using dateByddingTimeInterval to currentDate. 

NSDate *newDate = [currentDate dateByAddingTimeInterval:60*60*24*daysToAdd];


Complete code:

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]];

    int weekday = [comps weekday];

    NSDate *currentDate = [NSDate date];

   int daysToAdd = 0;

   if (weekday)
  {
        daysToAdd = mySelectedweekday-weekday;
  }
  else
  {
       daysToAdd = mySelectedweekday-weekday+7;
  }

NSDate *newDate = [currentDate dateByAddingTimeInterval:60*60*24*daysToAdd];

No comments:
Write comments