Ios 如何拆分包含时间值的字符串并将其转换以查找差异
注意:我使用共享知识功能作为一个整体回答了这个问题,而不仅仅是一个常见的问题——“拆分字符串” 我遇到过两个问题,但从来没有在一起: 如何分割字符串?(我承认这个问题已经被回答了好几次) 如何计算2次作为字符串和以时间格式(00:00:00)显示之间的差异Ios 如何拆分包含时间值的字符串并将其转换以查找差异,ios,objective-c,iphone,Ios,Objective C,Iphone,注意:我使用共享知识功能作为一个整体回答了这个问题,而不仅仅是一个常见的问题——“拆分字符串” 我遇到过两个问题,但从来没有在一起: 如何分割字符串?(我承认这个问题已经被回答了好几次) 如何计算2次作为字符串和以时间格式(00:00:00)显示之间的差异 这个问题的目的是帮助在核心数据中存储时间字符串的人能够计算两者之间的差异。我用“分享知识”功能回答了这个问题,但并没有简单地回答。请看下面 首先,我们像这样建立日期的格式。我使用的是HH:MM:SS格式。设置您的格式以适合保存的字符串 NSD
这个问题的目的是帮助在核心数据中存储时间字符串的人能够计算两者之间的差异。我用“分享知识”功能回答了这个问题,但并没有简单地回答。请看下面 首先,我们像这样建立日期的格式。我使用的是HH:MM:SS格式。设置您的格式以适合保存的字符串
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"HH:mm:ss"];
第二部分是引用字符串。我创建了一个新引用,只是为了在将字符串Date转换为NSDate时保持其整洁。在我的示例中,我在.h文件中声明了对核心数据的引用,因此我引用如下
NSString * StringTime1 = self.coreDataReference.time1;
NSString * StringTime2 = self.coreDataReference.time2;
将字符串转换为日期:
NSDate *time1 = [dateFormat dateFromString:StringTime1];
NSDate *time2 = [dateFormat dateFromString:StringTime2];
接下来,我通过得到两次之间的时间间隔来得到差异。我正在比较最近保存的时间(time2)和第一次保存的时间(time1)。这种方法本质上是从第一个时间减去第二个分配的时间,在这种情况下,tim2减去time1。你马上就会明白为什么了
NSTimeInterval distanceBetweenDates = [time2 timeIntervalSinceDate:time1];
出于显示目的,我将现在为双精度的时间(NSTimeInterval为双精度,请查看更多信息)转换为小时、分钟和秒值
int timeInt = (int) distanceBetweenDates;
//I convert the values into the time value
int hours = timeInt / 3600;
int minutes = (timeInt / 60) % 60;
int seconds = timeInt % 60;
最后,我们在日志中显示结果,如果您在那里也有标签显示
NSLog(@"The total time is: %02u:%02u:%02u", hours, minutes, seconds);
NSString * time = [NSString stringWithFormat:@"%02u:%02u:%02u", hours, minutes, seconds];
self.totalLoginTimeLabel.text = time;
我希望这有助于任何人的调查
整个代码如下:
-(void) calculateTimeIntervalFromStrings{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"HH:mm:ss"];
NSString * StringTime1 = self.coreDataReference.time1;
NSString * StringTime2 = self.coreDataReference.time2;
NSDate *time1 = [dateFormat dateFromString:StringTime1];
NSDate *time2 = [dateFormat dateFromString:StringTime2];
NSTimeInterval distanceBetweenDates = [time2 timeIntervalSinceDate:time1];
int timeInt = (int) distanceBetweenDates;
//I convert the values into the time value
int hours = timeInt / 3600;
int minutes = (timeInt / 60) % 60;
int seconds = timeInt % 60;
NSLog(@"The total time is: %02u:%02u:%02u", hours, minutes, seconds);
NSString * time = [NSString stringWithFormat:@"%02u:%02u:%02u", hours, minutes, seconds];
self.timeDifferenceLabel.text = time;
}
要调用此方法,请在viewDidLoad方法中如下放置:
[self calculateTimeIntervalFromStrings];
由于您来自字符串,因此需要将它们更改为NSDate对象。您将使用格式化程序来执行此操作
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"HH:mm:ss"];
// I don't know what date format you're coming from, you should of course use yours. check out the doc for more info.
NSDate *date1 = [dateFormat dateFromString:string1];
NSDate *date2 = [dateFormat dateFromString:string2];
现在有了来自字符串的日期对象。
如果您想知道时间日期之间的时间(秒或其他)
(来自)
NSTimeInterval提供以秒为单位的差值,如果需要小时、天等,可以从中计算
但有时,你可能只需要知道一个日期是在另一个日期之前还是之后。然后,您应该使用date compare,它返回OrderingDesc或Asc或相同的值,如下所示:
//Then the comparison will tell which is earlier/later/same:
if ([date1 compare:date2] == NSOrderedDescending) {
NSLog(@"date1 is later than date2");
} else if ([date1 compare:date2] == NSOrderedAscending) {
NSLog(@"date1 is earlier than date2");
} else {
NSLog(@"dates are the same");
}
你不应该使用这么多的对象/变量,因为你可以使用NSTimeInterval来完成这项非常具体的工作。因为你已经在发布问题3秒钟后回答了自己的问题。因为它已经被问过很多次了,如果你查一下的话。因为你做错了。我使用“提问”底部的“分享你的知识”功能来分享我的答案。没有单独回答“嘿,我需要帮助,但让我自己回答”。对不起,有误会。也许StackExchange需要显示一个不同的颜色标题或一些类似的功能来通知问题和共享知识之间的区别。好吧,好吧,对不起,我有点粗鲁,但是这个问题可以通过问google(或者差不多)来回答,这就是我在不到5分钟内所做的(而且我远不是一个好的程序员)。我觉得这篇文章更像是一个复制品。尽管如此,我还是保持原样,没有必要再谈论这个:)我知道你试图提出一个规范的问题/答案,但是,这个问题仍然必须是一个有效的问题。在编辑之后,这甚至不是一个有效的问题。
//Then the comparison will tell which is earlier/later/same:
if ([date1 compare:date2] == NSOrderedDescending) {
NSLog(@"date1 is later than date2");
} else if ([date1 compare:date2] == NSOrderedAscending) {
NSLog(@"date1 is earlier than date2");
} else {
NSLog(@"dates are the same");
}