NSDateFormatter code to adjust the time format
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"];
Use [NSDate Date] to get the current time of the system
NSString *currentdatestr = [dateformatter stringfromdate:[nsdate Date]];
The output format is: 2010-10-27 10:22:13
NSLog (@ "%@", currentdatestr);
Alloc do not forget to use the object after the release
[Dateformatter release];
Nsdatecomponents
The nsdatecomponents encapsulates a date component in an extensible, object-oriented manner. It is used to make up the time of the date and time the component provides a specified date: Hours, minutes, seconds, days, months, years, and so on. It can also be used to specify the time, for example, 5 hours and 16 minutes. A Nsdatecomponents object does not need to define all the component realms. When a new instance of a nsdatecomponents is created, the date component is set to Nsundefineddatecomponent.
A Nsdatecomponents object itself is meaningless; you need to know what calendar it is for, and you need to know whether its value is a positive integer and what the value is.
An instance of Nsdatecomponents is not responsible for answering information other than a date, it needs to be initialized first. For example, if you initialize an object for May 6, 2004, its day of the week is nsundefineddatecomponent, not Thursday. To get the correct day of the week, you must create a Nscalendar calendar instance, create a NSDate object and use the Datefromcomponents: method, and then use Components:fromdate: Retrieve the flat weeks
Getting information about an nsdatecomponents Object
Gets the information for a Nsdatecomponents object
- –era era
- –year year
- –month Month
- –day days
- When –hour
- –minute min
- –second sec
- –week
- –weekday
- –weekdayordinal
- –quarter Quarter
Setting information for an nsdatecomponents Object
Set up information for a Nsdatecomponents object
- –setera:
- –setyear:
- –setmonth:
- –setday:
- –sethour:
- –setminute:
- –setsecond:
- –setweek:
- –setweekday:
- –setweekdayordinal:
- –setquarter:
The example is as follows: May 6, 2004 is the day of the week
Nsdatecomponents *comps = [[Nsdatecomponents alloc] init]; |
[Comps Setday:6]; |
[Comps Setmonth:5]; |
[Comps setyear:2004]; |
Nscalendar *gregorian = [[Nscalendar alloc] |
Initwithcalendaridentifier:nsgregoriancalendar]; |
NSDate *date = [Gregorian datefromcomponents:comps]; |
[Comps release]; |
Nsdatecomponents *weekdaycomponents = |
[Gregorian Components:nsweekdaycalendarunit Fromdate:date]; |
int weekday = [weekdaycomponents weekday]; NSLog (@ "%d", weekday); |
For more details, please refer to "Calendars, date components, and Calendar Units" in Date and time Programming Guide. Program guidelines for dates and times
An example: How much time to calculate the distance a day
nsdate* toDate = [[NSDate alloc] initwithstring:@ "2012-9-29 0:0:00 +0600"];
nsdate* startdate = [[NSDate alloc] init];
nscalendar* Chineseclendar = [[Nscalendar alloc] Initwithcalendaridentifier:nsgregoriancalendar];
Nsuinteger unitflags =
Nshourcalendarunit | Nsminutecalendarunit | Nssecondcalendarunit | Nsdaycalendarunit | Nsmonthcalendarunit | Nsyearcalendarunit;
Nsdatecomponents *cps = [Chineseclendar components:unitflags fromdate:startdate todate:todate options:0];
Nsinteger diffhour = [cps hour];
Nsinteger diffmin = [cps minute];
Nsinteger diffsec = [cps second];
Nsinteger diffday = [cps day];
Nsinteger Diffmon = [cps month];
Nsinteger diffyear = [cps year];
NSLog (@ "from-now-%@, Diff:years:%d Months:%d, days;%d, Hours:%d, mins:%d, sec:%d",
[toDate description], diffyear, Diffmon, Diffday, Diffhour, diffmin,diffsec);
[ToDate release];
[StartDate release];
[Chineseclendar release];
The use of NSDateFormatter and nsdatecomponents