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