how to add time in current time

10,617

Solution 1

Do you mean current time, as in now?

If so, this will do it for you:

NSDate *now = [NSDate date]; // Grab current time
NSDate *newDate = [now addTimeInterval:XXX] // Add XXX seconds to *now

Where XXX is the time in seconds.

Solution 2

You should not use #define kOneDay 86400

In timezones that have daylight saving, each year there is one day that only has 82800 seconds, and one day that has 90000 seconds.
And sometimes there is even a day that has 86401 seconds. (But I think the leap second is ignored by NSDateComponents too.)

If you want to do it right you should use NSDateComponents.

to add one day you use it like this:

NSDateComponents *offset = [[[NSDateComponents alloc] init] autorelease];
[offset setDay:1];
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:offset toDate:date options:0];

it is important to use setDay:1 and not setHour:24.


to add two weeks and three hours you would use this

NSDateComponents *offset = [[[NSDateComponents alloc] init] autorelease];
[offset setWeek:2];
[offset setHour:3];
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:offset toDate:date options:0];

You should get the idea. Start with the biggest unit of change and work your way down to the smallest.

Yes, it's a little bit more work than addTimeInterval: but addTimeInterval:hours*60*60 is wrong if you care for days, weeks and months.

Solution 3

'addTimeInterval:' is deprecated

You can use this now

mydate=[NSDate date];    
mydate = [mydate dateByAddingTimeInterval:XXX]; //XXX in seconds
Share:
10,617

Related videos on Youtube

Shishir.bobby
Author by

Shishir.bobby

From Java to Android to iOS App Developer to Project Manager(now).

Updated on June 04, 2022

Comments

  • Shishir.bobby
    Shishir.bobby almost 2 years

    I'm quite confused about this one.

    I want to grab, current time, than according to condition, I want to add the required time, to the current time. for example.

    current time  = 06:47:10 
    //or should i hv to change this format to "2011-03-26 06:47:10  GMT"
    
     if(a= 1 and b= min ) 
      { //add 1 min to
      current time 
      } 
      else if(a= 1 and b= hour) 
      { //add 1
       hour to current time 
      } 
     else if(a= 1 and b=week )  
     { //add 1
     week to current time  
     }
    

    Just need to add the output of the above condition to current time.

    Please guide me with this.

    Regards

  • Shishir.bobby
    Shishir.bobby about 13 years
    thanks rog.. yea, i mean, current time as in now. but how can i manage, min,hours,week and month with NSDate.if i want to add 1 min or 1 hour or 1 month, how should i add it to current time.?? regards
  • Matthias Bauch
    Matthias Bauch about 13 years
    #define kOneDay 86400 this is where all the glitches with daylight saving come from. Each year there is one day that only has 82800 seconds, and one day that has 90000 seconds. And sometimes there is a day that has 86401 seconds.
  • Rog
    Rog about 13 years
    @fluchtpunkt good point! how would you normally cater for these exceptions in a generic way?
  • Matthias Bauch
    Matthias Bauch about 13 years
    @Rog NSDateComponents. see my answer.
  • Sudhakar
    Sudhakar about 10 years
    addTimeInterval is Deprecated. NSDate *now = [NSDate date]; NSDate *newDate = [now dateByAddingTimeInterval:60*60];//1hour time

Related