需要建议关于时间比较使用NSDate [英] Need advice about time comparison using NSDate
本文介绍了需要建议关于时间比较使用NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在开发闹钟。
我想现在比较一个时间和setTime。
I am developing Alarm Clock. I want to compare a time now and setTime. Is it possible to compare in minutes only.
我的问题是NSDate会比较几秒钟,例如9:38:50不等于9:38:00。
My Problem is NSDate will compare in seconds, for example 9:38:50 is not equal to 9:38:00. how can I compare in minutes?
推荐答案
首先,将它们转换为UNIX时间戳,忽略毫秒。
First, convert them to UNIX timestamp ignoring milliseconds.
NSUInteger time1 = (NSUInteger)[date1 timeIntervalSince1970];
NSUInteger time2 = (NSUInteger)[date2 timeIntervalSince1970];
忽略秒数。
time1 = time1 - (time1 % 60);
time2 = time2 - (time2 % 60);
现在,您可以安全地比较它们了。
Now, you can safely compare them.
if (time1 == time2) {
NSLog(@"Bingo");
}
这篇关于需要建议关于时间比较使用NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文