简体   繁体   English

在Objective-C中两次比较最好/最简单的方法是什么?

[英]What's the best/easiest way to compare two times in Objective-C?

I've got a string representation of a time, like "11:13 AM." 我有一个时间的字符串表示,如“11:13 AM”。 This was produced using an NSDateFormatter and the stringFromDate: method. 这是使用NSDateFormatter和stringFromDate:方法生成的。

I'd like to compare this time to the current time, but when I use the dateFromString: method to turn the string back into a date, a year, month and day are added - which I don't want. 我想将这个时间与当前时间进行比较,但是当我使用dateFromString:方法将字符串转换回日期时,会添加年,月和日 - 这是我不想要的。 I just need to know if right now is < or > the time stored in the string. 我只需要知道现在是否</>存储在字符串中的时间。

What's going to be the best way to handle that? 什么是处理这个问题的最好方法? Thanks in advance for your help. 在此先感谢您的帮助。

Instead of using the string representation, use the NSDate you got from the picker. 而不是使用字符串表示,使用从选择器获得的NSDate。 You can convert that hour/min/sec using NSDateComponents, then also convert [NSDate date] to NSDateComponents. 您可以使用NSDateComponents转换该小时/分钟/秒,然后将[NSDate date]转换为NSDateComponents。 Compare the hours/minutes/seconds of the two sets of components. 比较两组组件的小时/分钟/秒。

EDIT -- use a utility function for things like this that converts the hr/min/sec components of NSDate into a secondsOfTheDay (seconds since midnight). 编辑 - 使用实用程序功能,将NSDate的hr / min / sec组件转换为secondsOfTheDay(午夜后的秒数)。

You can directly use two time of day values since they are both seconds since midnight. 您可以直接使用两个时间值,因为它们都是午夜后的秒数。 Simple integers can be easily compared and stored and manipulated. 简单的整数可以很容易地进行比较,存储和操作。 You don't have to use NSDate all the time. 您不必一直使用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;
}

If I understand the problem correctly, you're using the dateFromString: method of NSDateFormatter . 如果我正确理解了这个问题,那么你正在使用NSDateFormatterdateFromString:方法。 This is giving you the correct time, but with a default date of January 1, 1970, which is useless to compare against the current date/time. 这给了你正确的时间,但默认日期是1970年1月1日,这与当前日期/时间比较没用。

This is easy to solve. 这很容易解决。 Use setDefaultDate: to set the default date to today. 使用setDefaultDate:将默认日期设置为今天。

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];

Can you reuse the string formatter that you used to create the string? 您可以重用用于创建字符串的字符串格式化程序吗? So, let's say you created the string like this: 所以,假设你创建了这样的字符串:

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm a"];
NSString *dateAsString = [formatter stringFromDate:[NSDate date]];

You can get an NSDate like this: 你可以得到这样的NSDate:

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm a"];
NSDate *date = [formatter dateFromString:dateAsString];

The day, month, year and timezone information will not be kept, but you'll have an NSDate object with the values of 1/1/1970 and GMT for the timezone offset. 日期,月份,年份和时区信息将不会保留,但您将拥有一个NSDate对象,其值为1/1/1970,GMT为时区偏移量。

At this point you can use the compare: (which is typically reserved for sorting operations) or the laterDate: or earlierDate: methods. 此时,您可以使用compare :(通常保留用于排序操作)或laterDate:或earlyDate:方法。

Be careful using NSDateFormatter like this, as you may run into issues with internationalization. 小心使用这样的NSDateFormatter,因为您可能会遇到国际化问题。

If you need to add information about the current date to the date you get from dateFromString:, such as the month day and year, you'll need to use NSCalendar's dateByAddingComponents:toDate:options: method. 如果需要将有关当前日期的信息添加到dateFromString:中的日期,例如月份日和年份,则需要使用NSCalendar的dateByAddingComponents:toDate:options:方法。

如果字符串最初是从NSDate创建的,那么您将希望使用原始NSDateNSDatecompare:方法(或某些变体,例如earlierDate:laterDate:进行比较[NSDate date]

You should use 24-hour based NSDateFormatter and compare as strings ("09:00am" > "08:00pm", but "09:00" < "20:00") . 您应该使用基于24小时的NSDateFormatter并比较为字符串(“09:00 am”>“08:00 pm”,但“09:00”<“20:00”)。 But in general it is right to operate with NSDate instances instead of strings 但一般来说,使用NSDate实例而不是字符串操作是正确的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 释放Objective-C属性的最佳方法是什么? - What's the best way to release objective-c properties? 在Objective-C中处理数组的最简单方法是什么? - what is easiest way of working arrays in objective-c? 将 NSUInteger 与 objective-c 中的 int(例如 5)进行比较的最快方法是什么? - What's the quickest way to compare a NSUInteger with an int (e.g. 5) in objective-c? 从 Objective-C 移植到 C++ 的最佳方法是什么? - What is the best way to port from Objective-C to C++? 获取链接并消除所有混乱,仅关注内容-在Objective-C中做到这一点的最佳方法是什么? - Get a link and removing all the clutter and only focusing on the content - what's the best way to do this in Objective-C? 是否可以轻松检查数组是否在Objective-C中排序? 如果没有,那么靠我自己做的“最佳实践”方法是什么? - Is it possible to easily check if an array is sorted in Objective-C? And if not, what's the “Best Practice” way to do it on my own? 将个人的iTunes库集成到Objective-C应用程序中的最佳方法是什么? - What is the best way to integrate a person's iTunes Library in an Objective-C application? 在Objective-C中使所有不同的视图类实现相同方法的最佳方法是什么 - What's the best way to have all different view classes to implement the same method in Objective-C 在Objective-C中使函数共享数组的最佳方法是什么? - What's the best way to have functions share an array in Objective-C? 在Objective-C中实现此滚动UI的最佳方法是什么? - What is the best way to implement this scrolling UI in Objective-C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM