Calendars in iOS

Source: Internet
Author: User

iOS comes with three calendars, Gregorian, Buddhist and Japanese calendars, and to set up a calendar you can go to the settings-general-language and region-calendar setting, and the iphone that we use in China is set to the Gregorian calendar by default. While Thais use the iphone default settings calendar is the Buddhist calendar. This will lead to the same code in the domestic display normal, to Thailand as if crossing the general.

Issue: Use NSDate *today = [NSDate date]; The current time to get it is in the domestic display normal is 2017, but to Thailand has become 2,056 years, this is why? Even if the time zone difference, can not be so much worse??

After careful thinking, found in the language and region of the settings there is a place to set the calendar, into the Gregorian calendar to change the calendar, found that the original display of 2017 became 2056.

WORKAROUND: Provide a way to obtain the correct year, month, time, and so on in the Gregorian calendar, not limited by time zone, System calendar, localization, and so on.

Global.h

@property (strong,nonatomic) Nscalendar* * * * * * * * * Second

global.m

-(Nscalendar *) calendar{if(!_calendar) {#ifdef__iphone_8_0 _calendar= [[Nscalendar alloc] Initwithcalendaridentifier:nscalendaridentifiergregorian];//get the Gregorian calendar according to Identifer#else_calendar= [[Nscalendar alloc] Initwithcalendaridentifier:nsgregoriancalendar];//get the Gregorian calendar according to Identifer#endif_calendar.timezone= [Nstimezone Localtimezone];//Eliminate time zone differences_calendar.locale = [Nslocale Currentlocale];//eliminate localization differences, including languages, date formats, etc.    }        return_calendar;}-(Nsdatecomponents *) components{if(!_components) {NSDate*date =[NSDate Date]; _components= [Self.calendar components:nscalendarunityear | Nscalendarunitmonth | Nscalendarunitday | Nscalendarunithour | Nscalendarunitminute | Nscalendarunitsecond |Nscalendarunitweekday Fromdate:date]; }        return_components;}//returns the current year-(NSString *) year{if(!_year) {_year= [NSString stringWithFormat:@"%04ld", [kglobal.components year]]; }    return_year;}//returns the current month-(NSString *) month{if(!_month) {_month= [NSString stringWithFormat:@"%02ld", [kglobal.components month]]; }    return_month;}//returns the current numbered-(NSString *) day{if(!_day) {_day= [NSString stringWithFormat:@"%02ld", [Kglobal.components Day]]; }    return_day;}//returns the current hour-(NSString *) hour{if(!_hour) {_hour= [NSString stringWithFormat:@"%02ld", [Kglobal.components Hour]]; }    return_hour;}//returns the current minute-(NSString *) minute{if(!_minute) {_minute= [NSString stringWithFormat:@"%02ld", [kglobal.components minute]]; }    return_minute;}//returns the current second-(NSString *) second{if(!_second) {_second= [NSString stringWithFormat:@"%02ld", [kglobal.components second]]; }    return_second;}

Calendars in iOS

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.