I have time
in string
format for example 2:00
. I want to initialize it to NSDate
with present date
.
I tried doing
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setHour:2];
I am not able to compare this with a date object. Please help
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];
You should be able to compare with date after that.
Maybe you mean something like. I think will set using today's date and then you can create NSDateComponents from that and set 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 setSecond:30];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With