How can I reset the current date retrieved from [NSDate date] but then change the time to 10:00 in the morning.
Asked
Active
Viewed 1.1e+01k times
5 Answers
197
As with all date manipulation you have to use NSDateComponents and NSCalendar
NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
[components setHour:10];
NSDate *today10am = [calendar dateFromComponents:components];
in iOS8 Apple introduced a convenience method that saves a few lines of code:
NSDate *d = [calendar dateBySettingHour:10 minute:0 second:0 ofDate:[NSDate date] options:0];
Swift:
let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let now: NSDate! = NSDate()
let date10h = calendar.dateBySettingHour(10, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)!

Cœur
- 37,241
- 25
- 195
- 267

Matthias Bauch
- 89,811
- 20
- 225
- 247
-
Why don't you use [NSCalendar currentCalendar] ? It would make code more universal. – pronebird May 03 '14 at 14:35
-
3Please note that NSYearCalendarUnit, NSMonthCalendarUnit and NSDayCalendarUnit are deprecated. Instead please use NSCalendarUnitYear, NSCalendarUnitMonth and NSCalendarUnitDay. Otherwise we may get some garbage date value. – Pradeep Reddy Kypa Aug 23 '14 at 19:07
-
[More information and examples on the new iOS 8 `NSCalendar` APIs](http://pivotallabs.com/?p=32104). – Joe Masilotti Feb 10 '15 at 11:51
-
Heads up: This didn't work for me until I set the calendar's locale and the timezone. – kraftydevil Aug 14 '15 at 02:55
-
@kraftydevil Can you please tell, how you provided locale and timezone? – Dimple Shah Apr 21 '16 at 06:48
4
this nsdate
used different format:
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"MMM dd, yyyy HH:mm"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:@"MMM dd, yyyy"];
NSDate *parsed = [inFormat dateFromString:dateString];

alecxe
- 462,703
- 120
- 1,088
- 1,195

Bharat Chandera
- 57
- 2
- 3
-
You didn't actually answer the question, but I found it very useful anyway: thanks. :) – Bruno Belotti Jan 21 '15 at 03:42
3
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setHour:10];
NSDate *date = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
[comps release];
Not tested in xcode though :)

Stefan Ticu
- 2,093
- 1
- 12
- 21
-
5That won't work. You are adding 10 hours to the current date and time, not setting the time to 10:00. – Ole Begemann Apr 10 '11 at 12:21
-
Sorry, I misunderstood the question, the answer of @flucchtpunkt seems correct – Stefan Ticu Apr 10 '11 at 12:25
1
I just set the timezone with Matthias Bauch answer And it worked for me. else it was adding 18:30 min more.
let cal: NSCalendar = NSCalendar.currentCalendar()
cal.timeZone = NSTimeZone(forSecondsFromGMT: 0)
let newDate: NSDate = cal.dateBySettingHour(1, minute: 0, second: 0, ofDate: NSDate(), options: NSCalendarOptions())!

Rinku
- 910
- 13
- 28
0
You can use this method for any minute / hour / period (aka am/pm) combination:
- (NSDate *)todayModifiedWithHours:(NSString *)hours
minutes:(NSString *)minutes
andPeriod:(NSString *)period
{
NSDate *todayModified = NSDate.date;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSMinuteCalendarUnit fromDate:todayModified];
[components setMinute:minutes.intValue];
int hour = 0;
if ([period.uppercaseString isEqualToString:@"AM"]) {
if (hours.intValue == 12) {
hour = 0;
}
else {
hour = hours.intValue;
}
}
else if ([period.uppercaseString isEqualToString:@"PM"]) {
if (hours.intValue != 12) {
hour = hours.intValue + 12;
}
else {
hour = 12;
}
}
[components setHour:hour];
todayModified = [calendar dateFromComponents:components];
return todayModified;
}
Requested Example:
NSDate *todayAt10AM = [self todayModifiedWithHours:@"10"
minutes:@"00"
andPeriod:@"am"];

kraftydevil
- 5,144
- 6
- 43
- 65