Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not exactly sure what you are looking for. For what I understand, you want to build a date with the current year, month and day, but with your supplied time by parsing it from a string. If that is the case, as others have pointed out, you need to play with NSDateComponents.</p> <p>Based on your code I wrote these lines. They should build a date by merging two dates. The current one and the one you parsed.</p> <pre><code>// Get the full current date NSDate *date = [NSDate date]; // Get the current calendar NSCalendar *calendar = [NSCalendar currentCalendar]; // Split the date into components but only take the year, month and day and leave the rest behind NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date]; // Build the date formatter NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"h:mm a"]; // Convert the string time into an NSDate NSDate *time = [formatter dateFromString:@"10:45 AM"]; // Split this one in components as well but take the time part this time NSDateComponents *timeComponents = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit ) fromDate:time]; // Do some merging between the two date components dateComponents.hour = timeComponents.hour; dateComponents.minute = timeComponents.minute; // Extract the NSDate object again NSDate *result = [calendar dateFromComponents:dateComponents]; // Check if this was what you where looking for NSLog(@"%@",result); </code></pre> <p>Please be aware that this sample code is by far non-optimized. There are more crisp ways to obtain what you are looking for by using time intervals, but I felt like you wanted a dirty simple example on how to do components copy and paste and then extracting dates.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload