简体   繁体   中英

iOS 12 stringFromDate method returns incorrect value

In iOS 12, I have noticed that the [NSDateFormatter stringFromDate] method returns an incorrect value on a simulator iPad, but the correct value on a physical iPad.

Example code:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"c"];

NSDate *currentDate = [NSDate date];
// Returns correct value on both physical and simulator iPads (eg. Fri Oct 19 15:39:53 2018)

NSString *currentDateAsString = [dateFormat stringFromDate:currentDate];
// Physical iPad: 5 (correct)
// Simulator iPad: 6 (incorrect)

Both iPads have the same date/time settings (eg. same timezone). What is the cause of this bug?

Check the first weekday on your real and simulator iPads to see if they match:

NSCalendar.current.firstWeekday

and take that value into your calculations on the weekday taken from the date formatter.

The first day of week on iOS depends on the Region settings. Double check the settings in Settings -> General -> Language & Region -> Region on both your real and simulator iPads. Also, it may worth to check the first day of week on your Mac in System Preferences -> Language & Region -> First day of week too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM