Initialize an NSDate with a specific time

I have time in string format, for example 2:00 . I want to initialize it before NSDate using the current date . I tried to do

  NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setHour:2]; 

I cannot compare this to a date object. Please, help

+8
iphone nsdate
source share
5 answers

Try

 NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setDay:6]; [comps setMonth:5]; [comps setYear:2004]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *date = [gregorian dateFromComponents:comps]; 

After that, you can compare with the date.

Perhaps you mean something like this. I think it will be used using today's date, and then you can create NSDateComponents and set the time values.

 NSDate *today = [NSDate date]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today]; [weekdayComponents setHour:12]; [weekdayComponents setMinute:10]; [weekdayComponents setSeconds:30]; 
+30
source share

To initialize an NSDate with the current date of 2, you will have to split the NSDate with the components year, month and day, for example.

 NSDate *today = [NSDate date]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *weekdayComponents = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:today]; 

Set the clock to 2

 weekdayComponents.hour = 2; 

And then create a new NSDate with these components

 NSDate *todayAt2 = [gregorian dateFromComponents:weekdayComponents]; 
+3
source share

You need to use an NSCalendar object to convert NSDateComponents to NSDate . See Apple Documentation on Creating a Date from Components .

+2
source share
  func initializeDateWithTime(date:NSDate,hrs:Int,minutes:Int) -> NSDate{ let today = date let gregorian:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! let dateComponents = gregorian.components([.Year, .Month, .Day], fromDate: today) dateComponents.hour = hrs; dateComponents.minute = minute; let todayAtX = gregorian.dateFromComponents(dateComponents) return todayAtX! } 
+1
source share

Just combining some 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; } 
0
source share

All Articles