What is the best / easiest way to compare twice in Objective-C?

I have a string representation of time, for example "11:13 a.m." This was created using the NSDateFormatter and stringFromDate: method.

I would like to compare this time with the current time, but when I use the dateFromString: method to return the string to the date, the year, month and day are added - which I don't want. I just need to know if right now, or> the time stored in the line.

What would be the best way to handle this? Thanks in advance for your help.

+3
source share
5 answers

Instead of using a string representation, use the NSDate that you received from the collector. You can convert this hour / min / s using NSDateComponents and then convert [NSDate date] to NSDateComponents. Compare the hours / minutes / seconds of the two sets of components.

EDIT - use a utility function for such things that converts hr / min / sec components from NSDate to a stopwatch (in seconds since midnight).

You can directly use the two values โ€‹โ€‹of the time of day, since they are both seconds from midnight. Prime integers can be easily compared, stored and manipulated. You do not need to constantly use NSDate.

//---------------------------------------------------------------------------- // extracts hour/minutes/seconds from NSDate, converts to seconds since midnight //---------------------------------------------------------------------------- unsigned secondOfTheDay( NSDate* time ) { NSCalendar* curCalendar = [NSCalendar currentCalendar]; const unsigned units = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents* comps = [curCalendar components:units fromDate:time]; int hour = [comps hour]; int min = [comps minute]; int sec = [comps second]; return ((hour * 60) + min) * 60 + sec; } 
+9
source

If I understand the problem correctly, you are using the dateFromString: NSDateFormatter . This gives you the correct time, but with a default date of January 1, 1970, which is useless to compare with the current date / time.

It is easy to solve. Use setDefaultDate: to set the default date to date.

 NSDate *now = [NSDate date]; NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; [formatter setDateFormat:@"hh:mm a"]; [formatter setDefaultDate:now]; NSDate *theDate = [formatter dateFromString:@"11:13 AM"]; NSComparisonResult theResult = [theDate compare:now]; 
+4
source

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.

+2
source

If the string was originally created from NSDate , then you will want to use this original NSDate to compare with [NSDate date] using the NSDate compare: method (or some option, for example, or laterDate: .

0
source

You should use the 24-hour NSDateFormatter and compare as strings ("09:00 am"> "20:00 pm" but "09:00" and "20:00"). But, in general, working correctly with NSDate instances instead of strings

0
source

Source: https://habr.com/ru/post/1442504/


All Articles