0

I want to add 3 hours to my current date. I am using the following code to add hours but it adds 6 hours to the current date.

NSDate *n = [NSDate date];
int daysToAdd = 3;  // or 60 :-)

// set up date components
NSDateComponents *components1 = [[[NSDateComponents alloc] init] autorelease];
[components1 setHour:daysToAdd];

// create a calendar   
NSCalendar *gregorian12 = [NSCalendar currentCalendar];

NSDate *newDate2 = [gregorian12 dateByAddingComponents:components toDate:n options:0];
NSLog(@"Clean: %@", newDate2);

Result is :-

2013-09-10 12:25:24.752 project[1554:c07] Clean: 2013-09-10 06:55:15 +0000
Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
Rushabh
  • 3,208
  • 5
  • 28
  • 51
  • 3
    Your calculation looks correct (apart from naming the variable "daysToAdd" if it contains hours). - But note that NSLog() prints NSDate objects always in the GMT time zone. – Martin R Sep 10 '13 at 07:09
  • Days to add , but you `setHour` ? You want to add day or hour ? – Raptor Sep 10 '13 at 07:09
  • i want to add 3 hours in my current date – Rushabh Sep 10 '13 at 07:10
  • possible duplicate of [NSdate Assuming wrong time zone](http://stackoverflow.com/questions/9442126/nsdate-assuming-wrong-time-zone) – Martin R Sep 10 '13 at 07:10
  • `gregorian12 dateByAddingComponents:components` , is it a typing mistake or that you are adding the wrong component (components instead of components1) here because I just checked your code works fine. – Zen Sep 10 '13 at 07:12
  • 3
    If you think that the calculation is wrong then you should show the *input* (current date), the *output* (the calculated date) and the *expected output*. – Martin R Sep 10 '13 at 07:13
  • @MartinR thanks find out my solution please remove flag – Rushabh Sep 10 '13 at 07:17
  • @Rushabh: I cannot remove the flag. - If you found a solution then you can post it as an answer. – Martin R Sep 10 '13 at 07:23
  • @MartinR i am using Ravindhiran answer and its work :) – Rushabh Sep 10 '13 at 08:05

3 Answers3

5

Try this

NSString *strCurrentDate;
NSString *strNewDate;
NSDate *date = [NSDate date];
NSDateFormatter *df =[[NSDateFormatter alloc]init];
[df setDateStyle:NSDateFormatterMediumStyle];
[df setTimeStyle:NSDateFormatterMediumStyle];
strCurrentDate = [df stringFromDate:date];
NSLog(@"Current Date and Time: %@",strCurrentDate);
int hoursToAdd = 3;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setHour:hoursToAdd];
NSDate *newDate= [calendar dateByAddingComponents:components toDate:date options:0];
[df setDateStyle:NSDateFormatterMediumStyle];
[df setTimeStyle:NSDateFormatterMediumStyle];
strNewDate = [df stringFromDate:newDate];
NSLog(@"New Date and Time: %@",strNewDate);

Output

Current Date and Time: Sep 10, 2013, 1:15:41 PM
    New Date and Time: Sep 10, 2013, 4:15:41 PM
Ravindhiran
  • 5,304
  • 9
  • 50
  • 82
2
NSDate *CurrentTimedate = [NSDate date];    
NSLog(@"%@",CurrentTimedate);   
NSDate *newDate = [CurrentTimedate dateByAddingTimeInterval:60*60*3];
NSLog(@"%@",newDate);
Jitendra
  • 5,055
  • 2
  • 22
  • 42
  • 2
    Adding a time as a fixed number of seconds is almost never what you want, even when dealing with hours. Think about things like daylight savings. This kind of programmings is what causes peoples alarm to go off at the wrong time twice a year! – David Rönnqvist Sep 10 '13 at 08:20
0
NSDate *newDate = [oldDate dateByAddingTimeInterval:hrs*60*60];
vamsi575kg
  • 594
  • 1
  • 7
  • 23
  • 3
    Adding a time as a fixed number of seconds is almost never what you want, even when dealing with hours. Think about things like daylight savings. This kind of programmings is what causes peoples alarm to go off at the wrong time twice a year! – David Rönnqvist Sep 10 '13 at 08:20
  • 1
    @DavidRönnqvist: Actually it does *not* make a difference with hours (as far as my test shows). `dateByAddingTimeInterval:3*60*60` and `dateByAddingComponents:` produce exactly the same result, even if there is a DST transition in that time span. – Martin R Sep 10 '13 at 09:02
  • @DavidRönnqvist DST only changes the offset from UTC, not time itself. The real reason to not use these exact time offsets is leap seconds. – borrrden Sep 10 '13 at 09:14
  • @borrrden: Even with leap seconds I cannot notice any difference between the two calculation methods. Do you have any reference or example? - The Unix time does not count leap seconds, as far as I know. – Martin R Sep 10 '13 at 09:26
  • @MartinR Seems you are right "Due to [Unix time's] handling of leap seconds, it is neither a linear representation of time nor a true representation of UTC". That's disappointing. – borrrden Sep 10 '13 at 09:35