【问题标题】:Get the difference time between two NSString in Objective C获取Objective C中两个NSString之间的时间差
【发布时间】:2013-04-09 17:31:41
【问题描述】:

我有两个字符串来自我存储时间的服务器,我需要比较这两个字符串之间的时间间隔,以分钟为单位,如果有必要,以小时为单位。我应该转换为NSDate 还是使用NSString

NSStrings 看起来像:

NOW 14:22
LAST TIME 10:18

编辑 #1 既然大家都说我用NSDate,我把数据库里的数据转换成DATETIME,现在我可以用下面的代码比较两个NSDate:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"pt_BR"]];
    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];

    NSDate *currentDateTimeWithOffset = [NSDate dateWithTimeIntervalSinceNow:[[NSTimeZone localTimeZone] secondsFromGMT]];

    NSString *strDate = [dateFormatter stringFromDate:currentDateTimeWithOffset];
    NSDate * now = [dateFormatter dateFromString:strDate];
NSDate *date = [dateFormatter dateFromString:@"2013-04-09 12:10:18"];

NSLog(@"Difference %f",[now timeIntervalSinceDate:date]);

结果如下:

 Difference 864.000000
 NOW 2013-04-09 15:24:42 +0000
 LAST DATE 2013-04-09 12:10:18

正确吗?差异以秒为单位?

【问题讨论】:

  • 您有大约 5 种不同的选择。最简单的就是自己破解格式。或者你可以使用 NSDateFormatter,有或没有 NSCalendar。如果您等待足够长的时间,有人可能会想出一个正则表达式版本。如果您使用 NSDateFormatter/NSCalendar,请注意时区问题。

标签: ios objective-c nsstring nsdate


【解决方案1】:

如果格式变得比您显示的更复杂,请考虑使用 NSDateFormatter。举个简单的例子:

NSArray *hoursMins = [timeString componentsSeparatedByString:@":"];
NSInteger timeInMins = [hoursMins[0] intValue] * 60 + [hoursMins[1] intValue];

然后以分钟为单位减去两次。日期格式化方法如下所示:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm"];
NSDate *date = [df dateFromString:timeString];

您可以使用以下方法获得两个日期之间的秒数差异:

[date timerIntervalSinceDate:anotherDate];

【讨论】:

  • hourMins 数组中的值不会是字符串吗?您正在对计算中的值进行数学运算。
  • +1 表示 NSDateFormatter 示例,这是我将使用的方法。
  • 谢谢@danh,我认为您的解决方案几乎可以让我到达那里,编辑了问题以便更好地理解
  • 但请记住,任何 NSDateFormatter 解决方案都应注意时区和语言环境问题。
  • @darkman,我认为最好的做法是以 UTC 存储日期,并尽可能在 UTC 中进行日期数学/比较。然后仅将时区用作向特定区域设置的人显示日期的用户友好方式。
【解决方案2】:

NSDateFormatter 是“干净”的方式。但是,如果您正在寻找快速和肮脏的东西,并且这些是您得到的实际字符串(包括现在和上次),我只需使用特定的 NSRange 来提取小时和分钟并进行比较。

确保,尽管您检查了小时数,并且如果“现在”小时在“最后一次”小时之前,则将 24 加到现在以进行当天翻转。

【讨论】:

    【解决方案3】:

    只有NSStringNSDateFormatter用于获取NSDate

    NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
    NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    
    [rfc3339DateFormatter setLocale:enUSPOSIXLocale];
    [rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
    [rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    
    // Convert the RFC 3339 date time string to an NSDate.
    NSDate *date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];
    

    如果您有单独的小时、分钟和秒值,您可以使用NSCalendar

    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setYear:1965];
    [comps setMonth:1];
    [comps setDay:6];
    [comps setHour:14];
    [comps setMinute:10];
    [comps setSecond:0];
    NSDate *date = [gregorian dateFromComponents:comps];
    [comps release];
    

    一个完美的解决方案是使用时间戳而不是 NSString。不仅可以轻松地将时间戳转换为NSDate (NSDate +dateWithTimeIntervalSinceReferenceDate),然后在需要时转换为NSString,还可以通过减法获得时差。

    对于两个NSDate 对象,时间差是用NSDate -timeIntervalSinceDate 计算的。

    【讨论】:

    • 这个答案没有解决问题中的实际问题。
    • @rmaddy 这不是复制和粘贴解决方案,无论如何我都不会给出这个级别的问题。是的,对于编辑前后的问题都是如此。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    相关资源
    最近更新 更多