When developing iOS programs, it is sometimes necessary to adjust the time format to the format you want, which we can do with the NSDateFormatter class.
For example:
Instantiate a NSDateFormatter object nsdateformatter *dateformatter = [[NSDateFormatter alloc] init];//set the time format, where you can set the format you want [ Dateformatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];//with [NSDate Date] can get system current time nsstring *currentdatestr = [ Dateformatter stringfromdate:[nsdate date]];//output format: 2010-10-27 10:22:13nslog (@ "%@", currentdatestr);// Alloc after the non-use of objects do not forget release[dateformatter release];
Character description
(:)
The time separator character. In some locales, you can use other characters to represent time separators. The time separator separates hours, minutes, and seconds when formatting time values. The actual character used as the time separator in the formatted output is determined by the current culture value of your application.
(/)
The date separator character. In some locales, you can use other characters to represent the date separator. The date separator separates day, month, and year when formatting date values. The actual character used as the date separator in the formatted output is determined by the current culture of your application.
(%)
Used to indicate that no matter what letter is trailing, subsequent characters should be read in single-letter format. Also used to indicate that a single-letter format should be read in a user-defined format. For more detailed information, see the following.
D
Displays the day as a number without leading zeros (such as 1). If this is the only character in the user-defined number format, use%d.
Dd
Displays the day as a number with leading zeros (such as 01).
EEE
Displays the day as an abbreviated form (for example, Sun).
Eeee
Displays the day as the full name (for example, Sunday).
M
Displays the month as a number without leading zeros (for example, January is represented as 1). If this is the only character in a user-defined number format, use%M.
Mm
Displays the month as a number with leading zeros (for example, 01/12/01).
MMM
Displays the month as an abbreviated form (for example, Jan).
MMMM
Displays the month as a full month name (for example, January).
Gg
Displays the era/era string (for example, A.D.)
H
Use a 12-hour system to display the hour as a number without leading zeros (for example, 1:15:15 PM). If this is the only character in a user-defined number format, use%h.
hh
Displays the hour as a number with leading zeros (for example, 01:15:15 PM) using a 12-hour system.
H
Displays the hour as a number without leading zeros, using a 24-hour system (for example, 1:15:15). If this is the only character in a user-defined number format, use%H.
HH
Displays the hour as a number with leading zeros using a 24-hour system (for example, 01:15:15).
M
Displays the minute as a number without leading zeros (for example, 12:1:15). If this is the only character in a user-defined number format, use%m.
Mm
Displays the minute as a number with leading zeros (for example, 12:01:15).
S
Displays the seconds as a number without leading zeros (for example, 12:15:5). If this is the only character in a user-defined number format, use%s.
Ss
Displays the seconds as a number with leading zeros (for example, 12:15:05).
F
Displays the decimal part of the second. For example, the FF will be accurately displayed to 1% seconds, and the FFFF will be displayed exactly one out of 10,000 seconds. You can use up to seven F symbols in a user-defined format. If this is the only character in a user-defined number format, use%f.
T
Use a 12-hour system, and display uppercase A for any hour before noon, and display uppercase P for any hour between noon and 11:59 p.m. If this is the only character in a user-defined number format, use%t.
Tt
For locales that use the 12-hour system, uppercase AM is displayed for any hour before noon, and uppercase PM is displayed for any hour between noon and 11:59 p.m.
For locales that use the 24-hour format, no characters are displayed.
Y
Displays the year (0-9) as a number without leading zeros. If this is the only character in a user-defined number format, use%y.
Yy
Displays the year in a two-digit number format with a leading zero, if applicable.
yyy
Displays the year in four-digit number format.
yyyy
Displays the year in four-digit number format.
Z
Displays the time zone offset without leading zeros (for example,-8). If this is the only character in a user-defined number format, use%z.
Zz
Displays the time zone offset with leading zeros (for example-08)
zzz
Displays the full time zone offset (for example, -08:00)
Format display
M/d/yy
12/7/58
D-mmm
7-dec
D-mmmm-yy
7-december-58
D MMMM
7 December
MMMM yy
December 58
HH:MM TT
08:50 PM
H:mm:ss T
8:50:35 P
h:mm
20:50
H:mm:ss
20:50:35
M/d/yyyy h:mm
12/7/1958 20:50
IOS NSDate Date Operations Summary
1//Current time Create NSDate
NSDate *mydate = [NSDate Date];
NSLog (@ "mydate =%@", mydate);
2//24 hours starting from now
Nstimeinterval secondsperday = 24*60*60;
NSDate *tomorrow = [NSDate datewithtimeintervalsincenow:secondsperday];
NSLog (@ "mydate =%@", tomorrow);
3//Date created based on an existing date
Nstimeinterval secondsPerDay1 = 24*60*60;
NSDate *now = [NSDate Date];
NSDate *yesterday = [now addtimeinterval:-secondsperday1];
NSLog (@ "Yesterday =%@", yesterday);
4//Compare Dates
BOOL samedate = [now isequaltodate:yesterday];
NSLog (@ "samedate =%lu", samedate);
4.1//get an earlier date
NSDate *earlierdate = [Yesterday Earlierdate:now];
NSLog (@ "earlierdate =%@", earlierdate);
4.2//later date
NSDate *laterdate = [Yesterday Laterdate:now];
NSLog (@ "laterdate =%@", laterdate);
Number of seconds between two dates
Nstimeinterval secondsbetweendates= [Yesterday Timeintervalsincedate:now];
NSLog (@ "secondsbetweendates=%lf", secondsbetweendates);
To create a date by using the Nscalendar class
Nsdatecomponents *comp = [[Nsdatecomponentsalloc]init];
[Comp setmonth:06];
[Comp setday:01];
[Comp setyear:2001];
Nscalendar *mycal = [[Nscalendaralloc]initwithcalendaridentifier:nsgregoriancalendar];
NSDate *mydate1 = [MyCal Datefromcomponents:comp];
NSLog (@ "myDate1 =%@", myDate1);
Get a date from an existing date
unsigned units = nsmonthcalendarunit| Nsdaycalendarunit| Nsyearcalendarunit;
Nsdatecomponents *COMP1 = [MyCal components:units fromdate:now];
Nsinteger month = [COMP1 month];
Nsinteger year = [COMP1 year];
Nsinteger day = [Comp1 day];
NSDateFormatter output of the implementation date
NSDateFormatter *formatter = [[Nsdateformatteralloc]init];
[Formatter setdatestyle:nsdateformatterfullstyle];//Direct output is machine code
or manually set the style [formatter setdateformat:@ "YYYY-MM-DD"];
NSString *string = [Formatter stringfromdate:now];
NSLog (@ "string =%@", string);
NSLog (@ "formater =%@", formatter);
Get date Format Object
-(NSDateFormatter *) dateformatter {
If (dateformatter = = Nil) {
Dateformatter = [[nsdateformatter alloc] init];
[Dateformatter setdatestyle: Nsdateformattermediumstyle];
[Dateformatter settimestyle: Nsdateformatternostyle];
}
return dateformatter;
}
The related usage of nsdate and NSDateFormatter
1.NSDateFormatter coordination between NSDate and NSString
NSDateFormatter has the following 2 methods:
-(NSString *) Stringfromdate: (NSDate *) date;//nsdate turn NSString
-(NSDate *) datefromstring: (NSString *) string;//nsstring turn nsdate
e.g.
NSString *[email protected] "1900-01-01";
NSDateFormatter *dateformatter=[[nsdateformatter Alloc]init];
[Dateformatter setdateformat:@ "Yyyy-mm-dd"];
NSDate *date=[dateformatter datefromstring:datestring];
[Dateformatter release];
NSString turn nsdate similar to above
NSString *datestring=[dateformatter datefromstring:[nsdate Date]];
Some formats for 2.NSDateFormatter are introduced
[Dateformatter setdateformat:@ "yyyy mm month DD day #eeee"]; Eeee is the day of the week and the Eee is the week
[Dateformatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];
[Dateformatter setdateformat:@ "yyyy year MMMMD Day"];//mmmm for xx months, a D can save 01th before the 0
3.NSString turn nsdate less one day solution
Use the following formatting method
1.[dateformatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];
2.
Nscalendar *calendar = [Nscalendar Currentcalendar];
nsdatecomponents *components = [Calendar components: (Nsdaycalendarunit | Nsmonthcalendarunit | Nsyearcalendarunit) fromdate:[nsdate [Date]];
NSDate *todaydate = [Calendar datefromcomponents:components];
Iosdate time Format (RPM)