在某些情况下,我收到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];
答案 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];