List of NSDateFormatter format details

Source: Internet
Author: User
Tags dateformat

Transferred from: http://www.cnblogs.com/xinus/archive/2012/10/29/NSDateFormatter_samples.html

Introduction: NSDateFormatter is a very common class used in iOS development to format NSDate objects and support localized information. Time-related features may also use the Nsdatecomponents class and the Nscalendar class. This article mainly lists common uses of NSDateFormatter.

The NSDate object contains two parts, date, and time. The formatted time string is mainly for the date and time. [Arc is turned on in the code below, so there is no release.] ]

1. Basic usage

1 nsdate* now = [NSDate date];2 nsdateformatter* FMT = [[NSDateFormatter alloc] init];3 Fmt.datestyle = Kcfdateformattersh Ortstyle;4 Fmt.timestyle = kcfdateformattershortstyle;5 Fmt.locale = [[Nslocale alloc] initwithlocaleidentifier:@ "En_ US "];6 nsstring* datestring = [FMT stringfromdate:now];7 NSLog (@"%@ ", 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:

typedef cf_enum (Cfindex, Cfdateformatterstyle) {    //date and time format styles    Kcfdateformatternostyle = 0,       //No output    Kcfdateformattershortstyle = 1,    //10/29/12, 2:27 PM    kcfdateformattermediumstyle = 2,   // Oct, 2:36:59 pm    kcfdateformatterlongstyle = 3,     //October,, 2:38:46 pm gmt+08:00    Kcfdatefo Rmatterfullstyle = 4      //Monday, October, 2:39:56 PM China Standard Time};

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:

typedef cf_enum (Cfindex, Cfdateformatterstyle) {    //date and time format styles    Kcfdateformatternostyle = 0,       //No output    Kcfdateformattershortstyle = 1,    //12-10-29 pm 2:52    kcfdateformattermediumstyle = 2,   // 2012-10-29 pm 2:51:43    kcfdateformatterlongstyle = 3,     //October 29, 2012 gmt+0800 2:51 P.M. 08 Sec.    Kcfdateformatterfullstyle = 4      //October 29, 2012 Monday China standard Time 2:46 P.M. 49 seconds};

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:

1 nsdate* now = [NSDate date];2 nsdateformatter* FMT = [[NSDateFormatter alloc] init];3 fmt.locale = [[Nslocale alloc] Ini twithlocaleidentifier:@ "ZH_CN"];4 Fmt.dateformat = @ "Yyyy-mm-dd ' T ' HH:mm:ss"; 5 nsstring* datestring = [FMT Stringfromdate:now];6 NSLog (@ "%@", 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.

1 nsdate* now = [NSDate date];2 nsdateformatter* FMT = [[NSDateFormatter alloc] init];3 fmt.locale = [[Nslocale alloc] Ini twithlocaleidentifier:@ "ZH_CN"];4 Fmt.dateformat = @ "Yyyy-mm-dd a HH:mm:ss eeee"; 5 nsstring* datestring = [FMT stringFrom Date:now];6 NSLog (@ "\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:

List of NSDateFormatter format details

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.