NSDate/NSDateFormatter - 只存储时间,不存储日期?
我一直在环顾四周,但没有看到任何可以解决这个问题的东西,所以我希望有人可以帮助我解决这个问题。我想做的是使用 NSDate 变量(在核心数据中)来存储时间,而不是日期和时间,而只是格式为 HH:MM:SS 的时间;
查看 NSDateFormatter 类参考和提供的示例代码后,我能够对其进行调整,并认为我的代码应该如下所示:
NSDateFormatter *timeOfArrival = [[NSDateFormatter alloc] init];
[timeOfArrival setTimeStyle:NSDateFormatterLongStyle];
[timeOfArrival setDateStyle:NSDateFormatterNoStyle];
[timeOfArrival setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
NSString *etaStr = [attributeDict objectForKey:@"cumulativeTime"];
NSLog(@"%@", etaStr);
checkpoint.eta = [timeOfArrival dateFromString:etaStr];
直到我尝试从字符串创建 NSDate 对象的最后一行为止的所有内容都有效,但是之后,checkpoint.eta 仍然为零。
例如,etaStr 正确输出了我的预期值 00:14:00,但我不确定我做错了什么。
I've been looking around but I haven't seen anything that addresses this so I'm hoping someone can help clear this up for me. What I am trying to do is use an NSDate variable(in core data) to store a time, not date and time, but just time in the format HH:MM:SS;
After looking at the NSDateFormatter class reference and the sample code provided I was able to tweak it and think that my code should look something like the following:
NSDateFormatter *timeOfArrival = [[NSDateFormatter alloc] init];
[timeOfArrival setTimeStyle:NSDateFormatterLongStyle];
[timeOfArrival setDateStyle:NSDateFormatterNoStyle];
[timeOfArrival setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
NSString *etaStr = [attributeDict objectForKey:@"cumulativeTime"];
NSLog(@"%@", etaStr);
checkpoint.eta = [timeOfArrival dateFromString:etaStr];
Everything up until the last line where I try to create my NSDate object from my string works, but after that, checkpoint.eta is still nil.
etaStr correctly outputs my expected value, 00:14:00, for example, but I'm not sure what I'm doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
设置区域设置和日期格式后,您应该能够在日期和字符串之间相互转换。因为您只需要时间,所以可以忽略日期部分。
输出
Swift 版本
是时候更新 swift 的答案了:
Swift 3 版本
After setting the locale and the date format you should be able to convert from date to string and back. Because you just need the time, you can ignore the date part.
Output
Swift version
Time to update this answer for swift:
Swift 3 version
经过一番思考并尝试了 Mundi 的答案后,看起来 Mundi 是从字符串创建字符串,而不创建 NSDate 或与 NSDate 相互转换。我还需要存储一个 NSDate ,所以这里是你如何能够相当轻松地得到你想要的东西:
Mundi 的答案有效,所以有人应该投票赞成他的答案,因为我投票太快,没有考虑到留下日期 @"1/ 21/13 00:14:00”在这种情况下并不重要,但他应该在其前面放置一个日期,以明确表示不输出该日期。来自网络服务或其他对象的某人变量将具有日期,然后@“HH:mm:ss a”将仅提取时间。这也可以帮助那些需要在其日期或时间了解上午/下午的人。
After thinking this through a bit, and trying Mundi's answer, it looked like Mundi was creating a string from a string without creating an NSDate or converting to or from an NSDate. I needed to store an NSDate as well, so here's how you can get what you want fairly easily:
Mundi's answer works, so someone should upvote his answer since I down voted too fast without taking into account that leaving off the date @"1/21/13 00:14:00" doesn't really matter in this case, but he should have put a date in front of it to make it clear that the date isn't output. Someone's variable from a web service or some other object would have the date, then the @"HH:mm:ss a" would pull out the time only. This also helps those who need the AM/PM on their date or time.
斯威夫特4
如果您想使用今天日期的时间,
如果您想在任何日期添加时间,那么:-
Swift 4
If you want to use time with today's date
if you want to add time in any date then:-
我最终没有使用 NSDate/NSDateFormatter 因为我无法让它正常工作。我的解决方案包括将时间字符串解析为小时、分钟和秒。我最终将所有内容都转换为秒,并以这种方式存储它们。
I ended up not using NSDate/NSDateFormatter because I couldn't get it to work properly. My solution consisted of parsing my time string into a hours, minutes, and seconds. I eventually ended up converting everything to seconds, and storing them in that way.