NSDate/NSDateFormatter - 只存储时间,不存储日期?

发布于 2024-12-01 00:51:04 字数 740 浏览 1 评论 0原文

我一直在环顾四周,但没有看到任何可以解决这个问题的东西,所以我希望有人可以帮助我解决这个问题。我想做的是使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

╄→承喏 2024-12-08 00:51:04

设置区域设置和日期格式后,您应该能够在日期和字符串之间相互转换。因为您只需要时间,所以可以忽略日期部分。

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];

输出

00:14:00

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

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 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:

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)
等数载,海棠开 2024-12-08 00:51:04

经过一番思考并尝试了 Mundi 的答案后,看起来 Mundi 是从字符串创建字符串,而不创建 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 的答案有效,所以有人应该投票赞成他的答案,因为我投票太快,没有考虑到留下日期 @"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:

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.

圈圈圆圆圈圈 2024-12-08 00:51:04

斯威夫特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)

如果您想在任何日期添加时间,那么:-

  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)

Swift 4
If you want to use time with today's date

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)
合久必婚 2024-12-08 00:51:04

我最终没有使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文