IOS Time zone settings

Source: Internet
Author: User
Tags time zones

Recently in the question of getting the current time, because our app is not necessarily used in the celestial territory, now many people go abroad to play, when they look at the app, if your app has involved in getting the current time, then you have to pay attention!

It seems to start from iOS8 (because there is no ios6,7 device at hand), the system acquisition time is based on the timezone , not the region , then in your phone settings, Universal--and date and time you will see the interface.

Here the system is automatically set by default, should be the system to obtain your current coordinates, to determine that your coordinates are in China, so the time zone is automatically set to Beijing.

But when you go to London to play? The system will get your coordinates, positioning as London, then your time will be set to London, UK.

Of course, you get to the right time locally. But when you open the app, if you don't have a time zone, you'll also get London time. No problem. But if you get the time to transfer back to your home, or compare it to the database, you have a string (Yyyy-mm-dd HH:mm:ss ), that's bad ...

In this case, the simplest way is to set the time zone, when your data need time to compare, you'd better put the time zone Dingzhun in a certain time zone, usually located in the Celestial region (East 8).

There is a change on the web Nslocale set Identifier for @ "ZH-CN", and then Formatter.locale = locale, the pro-test is not good to make . Before you try, change the auto-capture time zone to manual and enter a different region, such as London. On the code:

1Nslocale * locale = [Nslocale localewithlocaleidentifier:@"ZH-CN"];2NSDateFormatter * Formatter =[[NSDateFormatter alloc]init];3Formatter.locale =locale;4Formatter.dateformat =@"YYYY-MM-DD Hh:mm:ss";5NSDate * Date5 =[NSDate Date];6NSString * Nowtime =[Formatter stringfromdate:date5];7NSLog (@"nowtime=%@", nowtime);

The time you get is definitely not Beijing time, why? "ZH-CN" is Simplified Chinese. So this setting, what is the specific use I did not see, but for the time is not affected.

So how to set the celestial time zone correctly? Nstimezone, say more useless, on the code:

 nsdate * Date = [NSDate Date];    NSDateFormatter  * DF = [[NSDateFormatter Alloc]init]; [DF Setdateformat:  @ " yyyy-mm-dd hh:mm:ss   " ];  //     set East Zone 8  [DF settimezone:[nstimezone timezoneforsecondsfromgmt:8  *3600   * Now = [DF Stringfromdate:date];  //     correct acquisition of celestial time  NSLog (@ " now=%@  , now); 

Nstimezone also has [Nstimezone Systemtimezone], [Nstimezone Defaulttimezone] methods for obtaining the time zone, both of which are obtained from the system time zone.

Most people's time zones are automatically acquired, so when your app gets the system time, if it needs local time, then don't worry. If you want to compare with Beijing time, please set the time zone so as not to cause unnecessary trouble to the users abroad.

IOS Time zone settings

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.