I have time
in string
format for example 2:00
. I want to initialize it to NSDate
with present date
.
I tried doin
Just bringing together some of the Objective C answers, I (now) have this helper function:
+ (NSDate*)dateForTodayAtHour:(NSUInteger)hour andMinutes:(NSUInteger)minutes {
NSDate* today = [NSDate date];
NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents* weekdayComponents = [gregorian components:(NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:today];
[weekdayComponents setHour:hour];
[weekdayComponents setMinute:minutes];
[weekdayComponents setSecond:0];
NSDate* result = [gregorian dateFromComponents:weekdayComponents];
return result;
}