-5

I have an app in I need the date of the next day though I have looked around and found nothing on how to do this.

Edit:

Found the answer at last. Here is what I did:

NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDate *startOfToday;
NSDate *tomorrow;
NSTimeInterval interval;
[cal rangeOfUnit:NSDayCalendarUnit
       startDate:&startOfToday
        interval:&interval
         forDate:now];

tomorrow = [startOfToday dateByAddingTimeInterval:interval];
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"MM-dd-yy"];
tommorowDate = [df stringFromDate:tomorrow];

Though this was all thanks to one of the answers

Hive7
  • 3,599
  • 2
  • 22
  • 38
  • 1. Get current date 2. Add 1 day. 3. Check year, month, day components? – David Rönnqvist Aug 30 '13 at 13:31
  • 2
    I trivially found a solution (linked above) to this, so perhaps you need to learn how to look. – John Parker Aug 30 '13 at 13:32
  • @middaparka that gives the time as well – Hive7 Aug 30 '13 at 13:35
  • @OllieCole: NSDate will ALWAYS have time components. It represents a moment in time. – vikingosegundo Aug 30 '13 at 13:36
  • Ho do get rid of them @vikingosegundo – Hive7 Aug 30 '13 at 13:37
  • so you understand the word ALLWAYS? – vikingosegundo Aug 30 '13 at 13:38
  • yeh though is there a way to not display them @vikingosegundo – Hive7 Aug 30 '13 at 13:39
  • 1
    You could then use an [NSDateFormatter](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html) to obtain the portions of the date information you want, but that answer will correctly get the "next" date. Incidentally, you clearly need to spend some time taking a look at the Apple docs. – John Parker Aug 30 '13 at 13:39
  • how would I set the next days date to an nsdateformatter cos if I can make into a string then I can split it and use the splitted date – Hive7 Aug 30 '13 at 13:45
  • Do you really expect to be spoon-fed everything? Read the docs and search around the topic on Google/StackOverflow. – John Parker Aug 30 '13 at 13:49
  • Hey I am new to c so don't know much @middaparka – Hive7 Aug 30 '13 at 13:51
  • Apple has some fantastic "getting started" docs such as [Programming with Objective-C](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html), [Start Developing iOS Apps Today](https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/chapters/Introduction.html), etc. You'll be doing yourself a massive favour if you read and understand these first. – John Parker Aug 30 '13 at 13:53
  • Ok I am past the getting started phase though not that good – Hive7 Aug 30 '13 at 13:56

1 Answers1

1
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDate *startOfToday;
NSDate *tomorrow;
NSTimeInterval interval;
[cal rangeOfUnit:NSDayCalendarUnit 
       startDate:&startOfToday 
        interval:&interval // interval will hold the duration of the day. DST-aware.
         forDate:now];

tomorrow = [startOfToday dateByAddingTimeInterval:interval];

tomorrow now holds an NSDate object representing 0:00 of the next day in the device's timezone.

NSDate is representing a moment in time. To display it without time portion, use a NSDateFormatter. There are plenty of examples around here.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178