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;
我一直在环顾四周,但我没有看到任何解决这个问题的事情,所以我希望有人可以帮我解决这个问题。我想要做的是使用NSDate变量(在核心数据中)来存储时间,而不是日期和时间,但只是时间格式为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类引用和提供的示例代码之后,我能够调整它并认为我的代码应该如下所示:
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.
直到我尝试从我的字符串创建我的NSDate对象的最后一行的一切工作,但在那之后,checkpoint.eta仍然是零。
etaStr correctly outputs my expected value, 00:14:00, for example, but I'm not sure what I'm doing wrong.
例如,etaStr正确输出我的期望值00:14:00,但我不确定我做错了什么。
3 个解决方案
#1
20
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.
设置区域设置和日期格式后,您应该能够从日期转换为字符串并返回。因为您只需要时间,所以可以忽略日期部分。
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[[NSLocale alloc]
initWithLocaleIdentifier:@"en_US"] autorelease]];
[formatter setDateFormat:@"HH:mm:ss"];
NSString *etaStr = @"00:14:00";
NSDate *generatedDate = [formatter dateFromString:etaStr];
NSLog(@"%@", [formatter stringFromDate:generatedDate]);
[formatter release];
Output
00:14:00
Swift version
Time to update this answer for swift:
是时候为swift更新这个答案了:
var formatter = NSDateFormatter()
formatter.locale = NSLocale(localeIdentifier: "en_US")
formatter.dateFormat = "HH:mm:ss"
let etaString = "00:14:00"
let generatedDate = formatter.dateFromString(etaString)!
let generatedString = formatter.stringFromDate(generatedDate)
println(generatedString)
Swift 3 version
var formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US")
formatter.dateFormat = "HH:mm:ss"
let etaString = "00:14:00"
let generatedDate = formatter.date(from: etaString)!
let generatedString = formatter.string(from: generatedDate)
print(generatedString)
#2
2
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的回答,看起来Mundi正在创建一个字符串中的字符串而不创建NSDate或转换为NSDate或从NSDate转换。我还需要存储一个NSDate,所以这里是你如何轻松获得你想要的东西:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy HH:mm:ss a"];
NSDate *eventDate = [dateFormat dateFromString:[attributeDict objectForKey:@"cumulativeTime"]];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[timeFormat setDateFormat:@"HH:mm:ss a"];
NSString *timeString = [timeFormat stringFromDate:eventDate];
NSLog(@"EventDate: %@",timeString);
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.
Mundi的回答是有效的,所以有人应该提出他的答案,因为我投票太快而没有考虑到在“1/21/13 00:14:00”之前离开约会在这种情况下并不重要,但他应该在它前面放了一个日期,以表明日期没有输出。来自Web服务或其他对象的某个变量将具有日期,那么@“HH:mm:ss a”将仅拉出时间。这也有助于那些在日期或时间需要AM / PM的人。
#3
-1
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.
我最终没有使用NSDate / NSDateFormatter因为我无法正常工作。我的解决方案包括将我的时间字符串解析为小时,分钟和秒。我最终将所有内容转换为秒,并以这种方式存储它们。
#1
20
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.
设置区域设置和日期格式后,您应该能够从日期转换为字符串并返回。因为您只需要时间,所以可以忽略日期部分。
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[[NSLocale alloc]
initWithLocaleIdentifier:@"en_US"] autorelease]];
[formatter setDateFormat:@"HH:mm:ss"];
NSString *etaStr = @"00:14:00";
NSDate *generatedDate = [formatter dateFromString:etaStr];
NSLog(@"%@", [formatter stringFromDate:generatedDate]);
[formatter release];
Output
00:14:00
Swift version
Time to update this answer for swift:
是时候为swift更新这个答案了:
var formatter = NSDateFormatter()
formatter.locale = NSLocale(localeIdentifier: "en_US")
formatter.dateFormat = "HH:mm:ss"
let etaString = "00:14:00"
let generatedDate = formatter.dateFromString(etaString)!
let generatedString = formatter.stringFromDate(generatedDate)
println(generatedString)
Swift 3 version
var formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US")
formatter.dateFormat = "HH:mm:ss"
let etaString = "00:14:00"
let generatedDate = formatter.date(from: etaString)!
let generatedString = formatter.string(from: generatedDate)
print(generatedString)
#2
2
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的回答,看起来Mundi正在创建一个字符串中的字符串而不创建NSDate或转换为NSDate或从NSDate转换。我还需要存储一个NSDate,所以这里是你如何轻松获得你想要的东西:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy HH:mm:ss a"];
NSDate *eventDate = [dateFormat dateFromString:[attributeDict objectForKey:@"cumulativeTime"]];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[timeFormat setDateFormat:@"HH:mm:ss a"];
NSString *timeString = [timeFormat stringFromDate:eventDate];
NSLog(@"EventDate: %@",timeString);
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.
Mundi的回答是有效的,所以有人应该提出他的答案,因为我投票太快而没有考虑到在“1/21/13 00:14:00”之前离开约会在这种情况下并不重要,但他应该在它前面放了一个日期,以表明日期没有输出。来自Web服务或其他对象的某个变量将具有日期,那么@“HH:mm:ss a”将仅拉出时间。这也有助于那些在日期或时间需要AM / PM的人。
#3
-1
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.
我最终没有使用NSDate / NSDateFormatter因为我无法正常工作。我的解决方案包括将我的时间字符串解析为小时,分钟和秒。我最终将所有内容转换为秒,并以这种方式存储它们。