Conversion between date and string in iOS

Source: Internet
Author: User

What you have to know

G: A.D., e.g. ad A.D.

YY: The latter 2 digits of the year

YYYY: Full year

MM: Month, display as 1-12

MMM: Month, abbreviated for the English month, such as Jan

MMMM: Month, display full name of the English month, such as Janualy

DD: day, 2 digits, such as 02

D: Day, 1-2-bit display, such as 2

EEE: Abbreviated day of the week, such as Sun

EEEE: Full written day of the week, such as Sunday

AA: Last afternoon, AM/PM

H: When, 24-hour, 0-23

K: When, 12-hour, 0-11

M: minutes, 1-2-bit

MM: Min., 2-bit

S: seconds, 1-2 bits

SS: Seconds, 2 bits

S: MS

1.date Turn string

NSDate *date=[nsdate Date];

NSLog (@ "%@", date);//2015-11-20 00:37:40 +0000

NSDateFormatter *dateformatter=[[nsdateformatter alloc]init];//Create a date formatter

[Email protected] "YYYY-MM-DD hh:mm:ss";//Specify Date formatted form

NSLog (@ "%@", [Dateformatter stringfromdate:date]);//2015-11-20 08:24:04

[Email protected] "YYYY-MM-DD";

NSLog (@ "%@", [Dateformatter stringfromdate:date]);//2015-11-20

[Email protected] "YYYY-MM-DD eeee aa HH:mm:ss";

NSLog (@ "%@", [Dateformatter stringfromdate:date]);//2015-11-20 Friday AM 08:30:28

[Email protected] "yyyy-mm-dd hh:mm:ss z";

NSLog (@ "%@", [Dateformatter stringfromdate:date]);//2015-11-20 08:42:22 gmt+8

In fact, date-to-string only needs to be formatted according to the format, but the string to date need to be noted

2.string Turn Date

NSString *[email protected] "2012-5-4 4:34:23";

[Email protected] "YYYY-MM-DD hh:mm:ss";

output:2012-05-03 20:34:23 +0000

/*

The type of conversion must be consistent with DateFormat

2012-->y

5->m

4->d

*/

NSString *[email protected] "2012-5-4 4:34:23";

[e-mail protected] "YYYY-MM-DD"///The back of the Hh:mm:ss do not write it OK?

[Email protected] "YYYY-MM-DD hh:mm:ss";//Conversion format

NSLog (@ "%@", [Dateformatter datefromstring:datestr]);

Conversion between date and string 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.