获取Objective C中两个NSString之间的时间差

时间:2021-08-09 00:14:44

I have two strings coming from my server in which I store the time, and I need to compare the time interval between those two, in minutes, and if it's necessary, in hours. Should I convert to NSDate or use NSString?

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

The NSStrings look like:

NSStrings看起来像:

NOW 14:22
LAST TIME 10:18

EDIT #1 Since everyone is saying me to use NSDate, i converted the data in my database to DATETIME, and now i can compare the two NSDate using the following code :

编辑#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]);

And the result is the following :

结果如下:

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

Is that correct? the Difference is in Seconds?

那是对的吗?差异在于秒?

3 个解决方案

#1


4  

If the format gets more complex than the one you've shown, consider using NSDateFormatter. For the simple example:

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

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

Then subtract the two times in minutes. The date formatter approach looks like this:

然后在几分钟内减去两次。日期格式化程序方法如下所示:

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

You can get a difference in seconds between two dates using:

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

[date timerIntervalSinceDate:anotherDate];

#2


0  

NSDateFormatter is the "clean" way to do it. But if you are looking for quick and dirty, and those are the actual strings you are getting (that include the NOW and LAST TIME), I'd just use a specific NSRange to pull the hours and minutes out and compare them.

NSDateFormatter是“干净”的方式。但是如果你正在寻找快速和肮脏的,那些是你得到的实际字符串(包括现在和最后时间),我只是使用特定的NSRange来拉出时间和分钟并比较它们。

Make sure, though that you check the hours, and if the "now" hour is before the "last time" hour, you add 24 to the now to throw in the day rollover.

确保,虽然您检查了小时数,如果“现在”小时是在“最后一次”小时之前,则向现在添加24以投掷当天翻转。

#3


-1  

With only NSString, NSDateFormatter is used to get NSDate

仅使用NSString,NSDateFormatter用于获取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];

If you had separate values for hours, minutes and seconds you could use NSCalendar

如果您有小时,分钟和秒的单独值,则可以使用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];

And a perfect solution would be to use timestamps instead of NSString. Not only it's easy to convert timestamps to NSDate (NSDate +dateWithTimeIntervalSinceReferenceDate)and then to NSString when needed, but you can also get the time difference by subtraction.

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

And with two NSDate objects the time difference is calculated with NSDate -timeIntervalSinceDate.

使用两个NSDate对象时,使用NSDate -t​​imeIntervalSinceDate计算时间差。

#1


4  

If the format gets more complex than the one you've shown, consider using NSDateFormatter. For the simple example:

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

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

Then subtract the two times in minutes. The date formatter approach looks like this:

然后在几分钟内减去两次。日期格式化程序方法如下所示:

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

You can get a difference in seconds between two dates using:

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

[date timerIntervalSinceDate:anotherDate];

#2


0  

NSDateFormatter is the "clean" way to do it. But if you are looking for quick and dirty, and those are the actual strings you are getting (that include the NOW and LAST TIME), I'd just use a specific NSRange to pull the hours and minutes out and compare them.

NSDateFormatter是“干净”的方式。但是如果你正在寻找快速和肮脏的,那些是你得到的实际字符串(包括现在和最后时间),我只是使用特定的NSRange来拉出时间和分钟并比较它们。

Make sure, though that you check the hours, and if the "now" hour is before the "last time" hour, you add 24 to the now to throw in the day rollover.

确保,虽然您检查了小时数,如果“现在”小时是在“最后一次”小时之前,则向现在添加24以投掷当天翻转。

#3


-1  

With only NSString, NSDateFormatter is used to get NSDate

仅使用NSString,NSDateFormatter用于获取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];

If you had separate values for hours, minutes and seconds you could use NSCalendar

如果您有小时,分钟和秒的单独值,则可以使用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];

And a perfect solution would be to use timestamps instead of NSString. Not only it's easy to convert timestamps to NSDate (NSDate +dateWithTimeIntervalSinceReferenceDate)and then to NSString when needed, but you can also get the time difference by subtraction.

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

And with two NSDate objects the time difference is calculated with NSDate -timeIntervalSinceDate.

使用两个NSDate对象时,使用NSDate -t​​imeIntervalSinceDate计算时间差。