26

how do I set an existing NSDate's time?

That is I have a date (say current date/time), but then want to set this to a specific time (e.g. 11.23am) say. What is the quickest way to do this in objective C?

I'm looking at NSCalendar and see methods such as dateByAddingComponents:toDate:options:, and dateFromComponents:, but these don't seem to quite hit the mark for what I need here. For example:

  1. dateFromComponents: - I would have to work out all components for this to work which seems like overkill
  2. dateByAddingComponents:toDate:options: - I don't want to ADD in my case but rather "set" the time.
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Greg
  • 34,042
  • 79
  • 253
  • 454

6 Answers6

72
NSDate *date = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date];
[components setHour: 7];
[components setMinute: 59];
[components setSecond: 17];

NSDate *newDate = [gregorian dateFromComponents: components];

I use NSUIntegerMax instead of specific OR-combined bit masks because it's easier, but if you want to specify which data components to receive, be my guest.

Yusuf K.
  • 4,195
  • 1
  • 33
  • 69
Alexsander Akers
  • 15,967
  • 12
  • 58
  • 83
  • How can I put this date instead of timer in `scheduletimeinterval` method of objective c. https://stackoverflow.com/questions/49857236/trigger-local-notifications-automatically-daily-on-dynamic-time-given-in-arrays – iOS Developer Apr 18 '18 at 11:42
11

iOS 7 Note: I'm putting this in a few of these questions because use of NSUIntegerMax no longer seems to work on iOS7, and using it caused me to chase my tail for about 3 hours. I just discovered that when I use NSUIntegerMax, NSDateComponentsignores set year. So for example this DOES NOT change the year:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:NSUIntegerMax fromDate:date];
[comps setYear:year];
return [gregorian dateFromComponents:comps];

Whereas this DOES:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger timeComps = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSTimeZoneCalendarUnit);
NSDateComponents *comps = [gregorian components:timeComps fromDate:date];
[comps setYear:year];
return [gregorian dateFromComponents:comps];

It may be an iOS7 bug or it may be that using NSUIntegerMax working was a fluke which no longer works. I will file a bug report and get a definitive answer.

Regardless, if you want to avoid unexpected consequences specify ALL the components else you might be chasing your tail!

Cliff Ribaudo
  • 8,932
  • 2
  • 55
  • 78
  • How can I put this date instead of timer in scheduletimeinterval method of objective c. https://stackoverflow.com/questions/49857236/trigger-local-notifications-automatically-daily-on-dynamic-time-given-in-arrays – iOS Developer Apr 18 '18 at 11:45
8

Swift 2.0 version of how you'd set a given date to 8am

extension NSDate {

func setTo8AM() -> NSDate {
    let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    let components = calendar.components(([.Day, .Month, .Year]), fromDate: self)
    components.hour = 8
    return calendar.dateFromComponents(components)!
}

}
Andrew Schreiber
  • 14,344
  • 6
  • 46
  • 53
  • How can I put this date instead of timer in scheduletimeinterval method of objective c. https://stackoverflow.com/questions/49857236/trigger-local-notifications-automatically-daily-on-dynamic-time-given-in-arrays – iOS Developer Apr 18 '18 at 11:45
4

Apply this to set the Time from an another date to an existing Date

NSDate *today = [NSDate date]; // from which i need only "Year" "Month" "Date"

NSCalendar *calendar = [NSCalendar currentCalendar]
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit ) fromDate:today];

NSDate *timeDate = datePicker.date; // from which i need only "Hours" "Minutes" and "Seconds"

NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:timeDate];

            [dateComponents setHour:[timeComponents hour]];
            [dateComponents setMinute:[timeComponents minute]];
            [dateComponents setSecond:[timeComponents second]];

NSDate *newDate = [calendar dateFromComponents:dateComponents]; // New Date with My "Year" "Month" "Date" "Hours" "Minutes" and "Seconds" 

            NSLog(@"New Date: %@",newDate);
Bala
  • 2,895
  • 1
  • 19
  • 60
3

Here's a more generic Swift (v2.1.1) extension that builds on Andrew Schreiber's helpful answer.

extension NSDate {
    func dateWithTime(hour: Int, minute: Int, second: Int) -> NSDate? {
        let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
        let components = calendar.components(([.Day, .Month, .Year]), fromDate: self)
        components.hour = hour
        components.minute = minute
        components.second = second
        let newDate = calendar.dateFromComponents(components)
        return newDate
    }
}
zekel
  • 9,227
  • 10
  • 65
  • 96
  • How can I put this date instead of timer in scheduletimeinterval method of objective c https://stackoverflow.com/questions/49857236/trigger-local-notifications-automatically-daily-on-dynamic-time-given-in-arrays – iOS Developer Apr 18 '18 at 11:47
1

A swift example. For my purposes, I wanted to get the date for around 12 noon tomorrow.

var tomorrow:NSDate = NSDate().dateByAddingTimeInterval(60*60*24); //60seconds*60minutes*12hours
var gregorian:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!;
var unit : NSCalendarUnit = (NSCalendarUnit.YearCalendarUnit|NSCalendarUnit.MonthCalendarUnit|NSCalendarUnit.DayCalendarUnit|NSCalendarUnit.HourCalendarUnit|NSCalendarUnit.MinuteCalendarUnit);
var comps:NSDateComponents = gregorian.components(unit, fromDate: tomorrow);
comps.setValue(12, forComponent: NSCalendarUnit.HourCalendarUnit);
comps.setValue(0, forComponent: NSCalendarUnit.MinuteCalendarUnit);
var noon_tomorrow : NSDate = gregorian.dateFromComponents(comps)!;
Chase Roberts
  • 9,082
  • 13
  • 73
  • 131
  • Any Help with that? http://stackoverflow.com/questions/31263690/how-to-set-current-time-in-swift – Thiha Aung Jul 07 '15 at 08:48
  • how to ut date components instead of a timer in objective c https://stackoverflow.com/questions/49857236/trigger-local-notifications-automatically-daily-on-dynamic-time-given-in-arrays – iOS Developer Apr 18 '18 at 11:48