NSDateFormatter将午夜显示为24而不是00

时间:2020-10-28 12:55:41

标签: objective-c nsdate nsdateformatter

在我的应用程序中,我使用NSDateNSDateformater以以下格式显示小时:HH:mm。例如,当我传递代表午夜的时间戳时:1603843200000 我希望将时间设为00:00,而我将获得24:00。我怎样才能将小时设为00:00?

这是我用来格式化时间戳的代码:

NSNumber* timeStamp = currentConsumption.timestamp; // 1603843200000
NSDate* date = [NSDate dateWithTimeIntervalSince1970:([timeStamp longLongValue]/1000)];
NSDateFormatter* df = [NSDateFormatter new];
df.dateFormat = self->graphDateFormat; // @"k:mm"
if(self->graphType == day){
      [df setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
}
return  [df stringFromDate:date];

1 个答案:

答案 0 :(得分:0)

问题是我将格式设置为k:mm而不是HH:mm