Can I reuse the string formatter that was used to create the string? So, let's say you created a line like this:
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init]; [formatter setDateFormat:@"HH:mm a"]; NSString *dateAsString = [formatter stringFromDate:[NSDate date]];
You can get NSDate as follows:
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init]; [formatter setDateFormat:@"HH:mm a"]; NSDate *date = [formatter dateFromString:dateAsString];
Information about the day, month, year and time zone will not be saved, but you will have an NSDate object with values โโ1/1/1970 and GMT to offset the time zone.
At this point, you can use comparison: (which is usually reserved for sorting operations) or laterDate: or earlyDate: methods.
Be careful when using NSDateFormatter like this, as you may run into problems with internationalization.
If you need to add information about the current date to the date that you get with dateFromString: for example, day of the month and year, you need to use NSCalendar dateByAddingComponents: toDate: options: method.
source share