iOS Development--nsdateformatter

Source: Internet
Author: User

The NSDate object contains two parts, date, and time. The formatted time string is mainly for the date and time

1. Basic usage

1nsdate* now =[NSDate Date];2nsdateformatter* FMT =[[NSDateFormatter alloc] init];3Fmt.datestyle =Kcfdateformattershortstyle;4Fmt.timestyle =Kcfdateformattershortstyle;5Fmt.locale = [[Nslocale alloc] Initwithlocaleidentifier:@"en_US"];6nsstring* datestring =[FMT Stringfromdate:now];7NSLog (@"%@", datestring);

Print output: 10/29/12, 2:27 PM
This uses the system to provide a formatted string, which is set through fmt.datestyle and fmt.timestyle . The parameters used in the instance are Kcfdateformattershortstyle, plus:

1typedef cf_enum (Cfindex, Cfdateformatterstyle) {//date and time format styles2Kcfdateformatternostyle =0,//No output3Kcfdateformattershortstyle =1,//10/29/12, 2:27 PM4Kcfdateformattermediumstyle =2,//Oct, 2:36:59 PM5Kcfdateformatterlongstyle =3,//October, 2:38:46 PM gmt+08:006Kcfdateformatterfullstyle =4      //Monday, October, 2:39:56 PM China7};

2. Customizing the regional language
As in the example above, we are using the regional language is en_US, which refers to American English. If we switch to Simplified Chinese, the code is:

1 fmt.locale = [[Nslocale alloc] Initwithlocaleidentifier:@ "zh_cn"];

Then the corresponding output is:

1typedef cf_enum (Cfindex, Cfdateformatterstyle) {//date and time format styles2Kcfdateformatternostyle =0,//No output3Kcfdateformattershortstyle =1,//12-10-29 pm 2:524Kcfdateformattermediumstyle =2,//2012-10-29 pm 2:51:435Kcfdateformatterlongstyle =3,//October 29, 2012 gmt+0800 2:51 P.M. 08 sec6Kcfdateformatterfullstyle =4      //Monday, October 29, 2012, China Standard Time 2:46 P.M. 49 seconds7};

World-wide regional language code, see International for Unicode (ICU), Http://userguide.icu-project.org/formatparse/datetime

3. Custom date and time formats
NSDateFormatter provides a custom date-time method, mainly by setting the property DateFormat, which is commonly set as follows:

1nsdate* now =[NSDate Date];2nsdateformatter* FMT =[[NSDateFormatter alloc] init];3Fmt.locale = [[Nslocale alloc] Initwithlocaleidentifier:@"ZH_CN"];4Fmt.dateformat =@"yyyy-mm-dd ' T ' HH:mm:ss";5nsstring* datestring =[FMT Stringfromdate:now];6NSLog (@"%@", datestring);

Print output: 2012-10-29t16:08:40

In addition to the listed above, you can also specify a number of formats, see Http://userguide.icu-project.org/formatparse/datetime.
You can also print out localized string information in conjunction with locale settings.

1nsdate* now =[NSDate Date];2nsdateformatter* FMT =[[NSDateFormatter alloc] init];3Fmt.locale = [[Nslocale alloc] Initwithlocaleidentifier:@"ZH_CN"];4Fmt.dateformat =@"yyyy-mm-dd a HH:mm:ss eeee";5nsstring* datestring =[FMT Stringfromdate:now];6NSLog (@"\n%@", datestring);

Print output: 2012-10-29 16:25:27 Mon

4. Custom month week and other characters
NSDateFormatter also provides the appropriate way to modify these characters. In general, use the default character below the corresponding region language to be OK. But you do have the need, and you can do it. The corresponding method is very numerous, as follows:

Managing AM and PM Symbols

    • –amsymbol
    • –setamsymbol:
    • –pmsymbol
    • –setpmsymbol:

Managing Weekday Symbols

    • –weekdaysymbols
    • –setweekdaysymbols:
    • –shortweekdaysymbols
    • –setshortweekdaysymbols:
    • –veryshortweekdaysymbols
    • –setveryshortweekdaysymbols:
    • –standaloneweekdaysymbols
    • –setstandaloneweekdaysymbols:
    • –shortstandaloneweekdaysymbols
    • –setshortstandaloneweekdaysymbols:
    • –veryshortstandaloneweekdaysymbols
    • –setveryshortstandaloneweekdaysymbols:

Managing Month Symbols

    • –monthsymbols
    • –setmonthsymbols:
    • –shortmonthsymbols
    • –setshortmonthsymbols:
    • –veryshortmonthsymbols
    • –setveryshortmonthsymbols:
    • –standalonemonthsymbols
    • –setstandalonemonthsymbols:
    • –shortstandalonemonthsymbols
    • –setshortstandalonemonthsymbols:
    • –veryshortstandalonemonthsymbols
    • –setveryshortstandalonemonthsymbols:

Managing Quarter Symbols

    • –quartersymbols
    • –setquartersymbols:
    • –shortquartersymbols
    • –setshortquartersymbols:
    • –standalonequartersymbols
    • –setstandalonequartersymbols:
    • –shortstandalonequartersymbols
    • –setshortstandalonequartersymbols:

Managing Era Symbols

      • –erasymbols
      • –seterasymbols:
      • –longerasymbols
      • –setlongerasymbols:

Zz:http://www.cnblogs.com/xinus/archive/2012/10/29/nsdateformatter_samples.html

iOS Development--nsdateformatter

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.