我正在努力寻找一种仅将 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);
}
但我得到的结果是:
periodDuration: 0001-12-31 22:24:04 +0000
firstPeriodTime: 0001-12-31 22:24:04 +0000
我期待的结果:
periodDuration: 45:00
firstPeriodTime: 08:00
我究竟做错了什么?我怎样才能解决这个问题?谢谢你。