Common methods for IOS time classes

Source: Internet
Author: User

// Current date before

nsdate *today = [nsdatedate];


// Time Zone

nstimezone *zone = [nstimezonesystemtimezone];


Set interval

_interval = [Zone secondsfromgmtfordate: Today];


// today + 8 hours (Shanghai, China time zone)

Today = [Today datebyaddingtimeinterval: _interval];


//Get Tomorrow's date

nsdate *tomorrowdate = [[nsdatealloc]initwithtimeinterval: 60 * sincedate: [nsdate date]];


Calendar

Nscalendar * Calendar = [nscalendarCurrentcalendar];


Comps can separate the time and date of the information

Nsdatecomponents *Comps = [Calendar Components:(Nsyearcalendarunit |Nsmonthcalendarunit |Nsdaycalendarunit |Nsweekcalendarunit |Nsweekdaycalendarunit |Nsweekdayordinalcalendarunit |Nshourcalendarunit)FromDate: Tomorrowdate];


Gets the current day of the week

Nsinteger Weekday = [compsweekday];


Convert NSString type to NSDate

nsdate *serverdate = [Datefomatterdatefromstring: servertimestr];

nsdate *createdate = [Datefomatterdatefromstring: createtimestr];

Figure out the interval of two date

Nstimeinterval betweentime = [serverdate timeintervalsincedate: CreateDate];


Separated from the standard format of the month and day time  

NSDateFormatter *datefomatter = [[nsdateformatteralloc]init];

[Datefomatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];


Change string to formatter form

nsdate *stringdate = [Datefomatter datefromstring: string];


Formatter for month and day

[DatefomatterSetdateformat:@ "MM-DD"];


Extract string in month and day

nsstring *str1 = [Datefomatter stringfromdate: stringdate];



Common methods for IOS time classes

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.