Time management is a headache. Why? Because there are some differences around the world, apple has a very good time handling mechanism, but without careful study, it’s easy to get stuck. The time we fetch directly from the system or interact with the backend is basically UTC. What is UTC? Let’s go back to junior high……
GMT & UTC
UTC’s predecessor: GMT, Greenwich Mean Time, also known as Universal Time. GMT is the time at noon when the sun crosses the Greenwich Meridian (prime Meridian). But GMT is no longer used as the world’s standard time because the Earth’s rotation is uneven and irregular.
So there’s an upgraded version, UTC, which stands for Coordinated Universal Time. UTC is a time measurement system that approximates GMT in time as closely as possible, based on atomic time seconds. To ensure that UTC does not differ from GMT by more than 0.9 seconds, a positive or negative leap second is added to UTC if necessary. UTC is now used as world standard time. Computer time is currently in UTC
The time zone
As the earth rotates from west to east, the east sees the sun before the west, and the east also sees the sun earlier than the west. In order to unify the world’s time, the International Longitude Conference of 1884 decided to divide the globe into 24 time zones (12 east and 12 West). The United Kingdom (the former site of the Greenwich Observatory) has a zero time zone (GMT+00), 1-12 east, 1-12 west, and Beijing, China, 8 east (GMT+08).
If it is 6 o ‘clock sharp in the UK, it is 6 o ‘clock sharp in GMT and 14 o ‘clock sharp in Beijing.
Since UTC and GMT are essentially the same (within 0.9 seconds), UTC time is converted in the same way as GMT.
With this knowledge, it’s easy to understand apple’s time management
Get the current time
The easiest way to get this is:
NSDate *nowTime = [NSDate date];
However, we get the time in the zero hour zone, and our Chinese time zone is in the east 8 zone, so we need to deal with this time.
- (NSString *)getNowDateToString{ NSDate *date = [NSDate date]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; If not, NSDateFormatter is automatically set to the current time zone of the phone. For consistency, it is best to write, so that no matter where our phone is located, Dateformat. timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"]; dateformat. timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"]; //dateFormat.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Tokyo"]; // dateformat. timeZone = [NSTimeZone timeZoneWithName:@"GMT"]; Dateformat. timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; [dateFormat setDateFormat:@" YYYY-MM-DD HH: MM :ss"]; NSString *dateString = [dateFormat stringFromDate:date]; return dateString; }Copy the code
The other is to get the time from the interface. In general, the interface returns the time with no time zone
Such as:
The 2020-12-23 18:47:28
When converting NSDate with NSDateFormatter, if TimeZone is not specified, then the default is UTC zero hour, so the result will be 8 hours less than the result of the interface:
The 2020-12-23 10:47:28
Then the same idea can be solved by writing it this way:
dateFormat.timeZone = [NSTimeZone timeZoneWithName:@”Asia/Shanghai”];
Equivalent to the time directly assigned to the east 8 zone.
Xcode has a pit here,
I’ll write it like this:
- (NSDate *)stringToNSDate (NSString *)dateString{NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"]; [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; return [formatter dateFromString:dateString]; }Copy the code
When I hover over the date variable, I get a printed value that is 8 hours less than the target time:
Wed Nov 11 10:20:30 2020
I fucking take it, didn’t print at the beginning, how to adjust all strange, fortunately I printed a = =
Ok, that’s all for today, and finally post a common conversion code
- (NSDate *)stringToNSDate:(NSString *)dateString{NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"]; [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; return [formatter dateFromString:dateString]; } //NSDate time to NSString format - (NSString *)dateToString:(NSDate *)date{NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; dateFormat.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"]; [dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; NSString *dateString = [dateFormat stringFromDate:date]; return dateString; }Copy the code