简体   繁体   English

NSDate / NSDateFormatter - 仅存储时间,而不是日期?

[英]NSDate/NSDateFormatter - Storing only time, not date?

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,但我不确定我做错了什么。

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 Swift版本

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 Swift 3版

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)

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. 经过一番思考后,尝试了Mundi的答案,看起来Mundi正在创建一个字符串中的字符串而不创建NSDate或转换为NSDate或从NSDate转换。 I needed to store an NSDate as well, so here's how you can get what you want fairly easily: 我也需要存储一个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. Mundi的回答是有效的,所以有人应该对他的答案进行投票,因为我投票太快而没有考虑到在“1/21/13 00:14:00”之前离开约会在这种情况下并不重要,但他应该在它前面放了一个日期,以表明日期没有输出。 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. 某个人来自Web服务或其他对象的变量会有日期,那么@“HH:mm:ss a”只会抽出时间。 This also helps those who need the AM/PM on their date or time. 这也有助于那些在日期或时间需要AM / PM的人。

Swift 4 If you want to use time with today's date Swift 4如果你想在今天的日期使用时间

let todate = Date()
        let formater = DateFormatter()
        formater.dateFormat = "yyyy:MM:dd"
       let date = formater.string(from: todate)
        print(date)
        let completeDate = date + ":9:00AM"
        print(completeDate)
        let anotherFormater = DateFormatter()
        anotherFormater.dateFormat = "EEEEE MMMMM yyyy HH:mm:ss.SSSZ"
       anotherFormater.date(from: completeDate)

if you want to add time in any date then:- 如果你想在任何日期增加时间,那么: -

  let completeDate = "2019-02-15" + ":9:00AM"
            print(completeDate)
            let anotherFormater = DateFormatter()
            anotherFormater.dateFormat = "EEEEE MMMMM yyyy HH:mm:ss.SSSZ"
           anotherFormater.date(from: completeDate)

I ended up not using NSDate/NSDateFormatter because I couldn't get it to work properly. 我最终没有使用NSDate / NSDateFormatter,因为我无法让它正常工作。 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. 我最终将所有内容转换为秒,并以这种方式存储它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM