NSDate当前日期返回零

时间:2017-10-09 09:09:40

标签: ios objective-c nsdate

在某些情况下,我收到currentDateString为零。我的应用程序处于正常状态,因此我无法找到确切的方案。

 NSDate *currentDate = [NSDate date];
 NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
 NSString *currentDateString =  [dateFormatter stringFromDate:currentDate];

1 个答案:

答案 0 :(得分:0)

我试过这段代码。我得到了正确的结果。请设置DateFormatter Locale

NSDate *currentDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *currentDateString =  [dateFormatter stringFromDate:currentDate];