IOS time operations to be updated

Source: Internet
Author: User
IPhone: nsdata year, month, week, day, hour, minute, second acquisition and nsdatatonsstring Method  

Obtain time information in nsdate, year, month, week, day, hour, minute, and second:

Nscalendar * Calendar = [[nscalendar alloc] initwithcalendaridentifier: nsgregoriancalendar];

Nsdate * now;

Nsdatecomponents * comps = [[nsdatecomponents alloc] init];

Nsinteger unitflags = nsyearcalendarunit | nsmonthcalendarunit | nsdaycalendarunit | nsweekdaycalendarunit |

Nshourcalendarunit | nsminutecalendarunit | nssecondcalendarunit;

Now = [nsdate date];

Comps = [calendar components: unitflags fromdate: Now];

Int year = [comps year];

Int week = [comps weekday];

Int month = [comps month];

Int day = [comps Day];

Int hour = [comps hour];

Int min = [comps minute];

Int sec = [comps second];

 

Nsdatatonsstring method:

 

-(Nsstring *) nsdatetonstring :( nsdate *) nsdate {

// Nsstring * string = [nsdate descriptionwithcalendarformat: @ "% Y/% m/% d % H: % m: % s" timezone: Nil locale: Nil]; this method has APIs. Do not use this method if you want to upload an app.

Nsdateformatter * FMt = [[nsdateformatter alloc] init] autorelease];

[FMT setdateformat: @ "yyyy/mm/dd hh: mm: SS"];

Nsstring * string = [FMT stringfromdate: nsdate];

Return string;

}

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.