xcode 尝试仅将 NSDate 用于时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16372122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:19:44  来源:igfitidea点击:

Trying to use NSDate for time only

iosobjective-cxcodensdatensdatecomponents

提问by Noam Solovechick

I'm struggling to find a way to use NSDate for time only purposes. I was trying to do the following code:

我正在努力寻找一种仅出于时间目的使用 NSDate 的方法。我试图执行以下代码:

- (void)awakeFromInsert
{
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    comps.minute = 45;
    comps.second = 0;

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    self.periodDuration = [calendar dateFromComponents:comps];
    NSLog(@"periodDuration: %@",self.periodDuration);

    comps = [[NSDateComponents alloc] init];
    comps.hour = 8;
    comps.minute = 0;
    self.firstPeriodStartTime = [calendar dateFromComponents:comps];
    NSLog(@"firstPeriodTime: %@",self.periodDuration);
}

But the result I get is:

但我得到的结果是:

periodDuration: 0001-12-31 22:24:04 +0000
firstPeriodTime: 0001-12-31 22:24:04 +0000

The result I was expecting:

我期待的结果:

periodDuration: 45:00
firstPeriodTime: 08:00

What am I doing wrong? How can I fix this? Thank you.

我究竟做错了什么?我怎样才能解决这个问题?谢谢你。

回答by Anupdas

The log of date is misleading you, if you are forming a date with only a few components you will not get a proper date instance which uniquely identifies a date and time.

日期日志会误导您,如果您形成的日期只有几个组件,您将无法获得唯一标识日期和时间的正确日期实例。

If you try with this code snipped you can find that if you are just converting the dates back to string it is just as you expect

如果您尝试使用此代码剪断,您会发现如果您只是将日期转换回字符串,它就像您期望的那样

NSDateComponents *comps = [[NSDateComponents alloc] init];
comps.minute = 45;
comps.second = 0;

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *periodDate = [calendar dateFromComponents:comps];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm:ss"];
NSLog(@"periodDuration: %@",[dateFormatter stringFromDate:periodDate]);

comps = [[NSDateComponents alloc] init];
comps.hour = 8;
comps.minute = 0;
NSDate *firstPeriodDate = [calendar dateFromComponents:comps];
[dateFormatter setDateFormat:@"HH:mm"];
NSLog(@"firstPeriodTime: %@",[dateFormatter stringFromDate:firstPeriodDate]);

periodDuration: 45:00

期间持续时间:45:00

firstPeriodTime: 08:00

第一期时间:08:00

回答by Ayush

You can use NSDateFormatter

您可以使用 NSDateFormatter

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];    
[dateFormatter setDateFormat:@"HH:mm"];
NSLog(@"%@",[dateFormatter stringFromDate:[NSDate date]]);

回答by Andreas Ley

Use a NSTimeInterval(which is, basically, a double containing the number of seconds). To store it in CoreData:

使用 a NSTimeInterval(基本上是包含秒数的双精度值)。将其存储在CoreData

[NSNumber numberWithDouble:myTimeInterval];

As for formatting: If you want more than 24h displayed, use this:

至于格式:如果要显示超过 24 小时,请使用:

NSUInteger seconds = (NSUInteger)round(myTimeInterval);
NSString *formattedDate = [NSString stringWithFormat:@"%02u:%02u:%02u",
                           seconds / 3600, (seconds / 60) % 60, seconds % 60];
NSLog(@"%@", formattedDate);

If you don't want more than 24h, you can use NSDateand NSDateFormatteragain:

如果你不希望超过24h,可以使用NSDateNSDateFormatter再次:

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:myTimeInterval];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
NSString *formattedDate = [dateFormatter stringFromDate:date];
NSLog(@"%@", formattedDate);

回答by Alex Zavatone

NSString *timeString;
timeString = [NSDateFormatter localizedStringFromDate:[NSDate date]
                                            dateStyle:NSDateFormatterNoStyle
                                            timeStyle:NSDateFormatterMediumStyle];

12:03:54 PM

下午 12:03:54