0

I am having a small problem with NSDateComponents.

What I have right now looks like this: [2014-8-08 08:00:00] because I'm using the current time initially (using [NSDate date]), and then setting the hours, minutes and seconds manually to something else through comps.

My objective is to separate the date from the time using a string. I still want to be able to use the current time, but when it comes to [comps set Hour/Minute/Second], I want to set them all using a string.

The string would be formatted like this: (06:00:00).

I need to set this as my default choice when opening NSDatePicker. So, it should look like [2014-8-08 06:00:00] initially when selecting a date.

Is there any way that I can set the hour/minute/second through a string?

I HAVE to use a string. the 06:00:00 resembles a MYSQL query for when a store opens it's doors. I am working with hundreds of stores. Each store has different opening times. Thanks

Franco

startTimeDatePicker.datePickerMode = UIDatePickerModeDateAndTime;
NSDate *now = [NSDate date];
startTimeDatePicker.maximumDate = now;


NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components: NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]];
[comps setHour:8];
[comps setMinute:0];
[comps setSecond:0];

NSDate *dateFromComps = [calendar dateFromComponents:comps]; 
francojay
  • 450
  • 3
  • 15
  • 1
    Why are you using a string? It's the wrong data type. – Droppy Aug 08 '14 at 07:19
  • Sorry for the repost. @Droppy I have to use a string. Yogesh, your answer did not work for me. The reason I have to use a string is because the 06:00:00 comes directly from MYSQL query. It resembles when the store opens it's doors. And I am doing this for hundreds of stores so they all open their doors at different times. – francojay Aug 08 '14 at 14:11
  • So the question is about parsing a string with "06:00:00" into integers of hour/minute/second. Nothing else. – Droppy Aug 08 '14 at 14:14
  • Yes I need the NSDatePicker to by default, set the hours/minutes/seconds to the query (06:00:00). There is also another NSDatePicker with the end time (store closes doors). So when a customer picks a date, the first date picker should be defaulted as (store opens) and second as (store closes) – francojay Aug 08 '14 at 14:21

1 Answers1

2

This is the correct answer. You must separate the string individually, and input them into the components.

NSArray *results = [storeOpen componentsSeparatedByString:@":"];
if ([results count] == 3) {
    NSLog(@"1 - %d", [[results objectAtIndex:0] intValue]);
    NSLog(@"2 - %d", [[results objectAtIndex:1] intValue]);
    NSLog(@"3 - %d", [[results objectAtIndex:2] intValue]);


NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components: NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]];
[comps setHour:[[results objectAtIndex:0] intValue]];
[comps setMinute:[[results objectAtIndex:1] intValue]];
[comps setSecond:[[results objectAtIndex:2] intValue]];


NSDate *dateFromComps = [calendar dateFromComponents:comps];
Machina
  • 252
  • 2
  • 13