nsdate *date = [NSDate date];
[Date Datebyaddingtimeinterval:]//Changes the value of the date by adding or minus the number of seconds
It is generally necessary to use the date formatter Nsformatter class Nsformatter *formatter = [Nsformatter alloc]init];
[Formatter Setdatestyle:]//Setting the system time format
[Formatter setdateformat:];//custom time format (format to specification)
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
[Formatter settimestyle:]//Set time format
Nslocale Area Category
Nslocale LOCA1 = [[Nslocale alloc]initwithlocaleidentifier:@ "Zh_ch"];
Nslocale LOCA2 = [[Nslocale alloc]initwithlocaleidentifier:@ "en_US"];
[Formattter setlocale]//Setting region format
NSString *str = [Formatter stringfromdate:date]//converts a date to a string by a set-up formatter
The same nsstring can also be converted to nsdate format using the format
NSDate *date1 = [formatter datefromstring:];
Nscalendar and Nsdatecomponents
Nscalendar Calendar =[nscalendar currentcalendar];//creating calendars from System calendar information
Sets a unitflags that contains the specified date component
unsigned unitflags =
Nscalendarunitera |
Nscalendarunityear |
Nscalendarunitmonth |
Nscalendarunitday |
Nscalendarunithour |
Nscalendarunitminute |
Nscalendarunitsecond |
Nscalendarunitweekday;
To get date components in the specified Nscalendar format component
nsdatecomponents comp = [Calendar components:unitflags fromdate:date];
You can read the Comp.era,comp.year,......
同样的NSComponents也可以用来组成一个NSDate
nsdatecomponents comp2 = [nsdatecomponents alloc]init];
Set COMP2
Comp2.year = 2016,comp2.month = 07,comp2.day =,........
Convert date components to date2 through calendar
NSDate date2 = [Calendar DATEFROMCOMPONENTS:COMP2];
NSLog (@ "Date:%@", date2);
Summary: NSDate and nsstring can be converted to each other through Nsformatter
NSDate and nscompoents can be converted to each other by Nscalendar
NSDate, Nscalendar some usage