Class and use of processing time in iOS
1. NSDate This class is to generate a date
If you use [NSDate Date] This most common method you can get the current time of the system (UTC time, regardless of your current time zone)
Like what:
NSDate *date = [NSDate Date];
NSLog (@ "%@", date);
Output:
2013-04-27 07:25:59 +0000
In the class header file NSDate.h, we can see all the methods and class methods, but there is no data, so we can print out the time of the string, but there is no way to get the information of the year, month, day and so on alone.
In Apple's official document, this class is used to describe a specific point in time, which means that a point in time can be recorded, and the data cannot be modified.
This class provides methods to get the time (past, current, future), calculate the interval between two times, compare time, describe the time content
2. NSDateFormatter
As the name implies, this class is a print display format that describes date, and works with the NSDate class.
Because different regions are accustomed to different formats, users will also set different formats, so iOS provides a very rich set of methods, display parameters, basically you can think of the method is clean sweep.
You can set the time display format, date format, time format, and time zone, and so on, the most convenient is that you can also set the AM/PM, month, week display, which is really very convenient in some special cases (the default function is very convenient)
–dateformat
–setdateformat:
–datestyle
–setdatestyle:
–timestyle
–settimestyle:
Some of the most common methods are as follows:
Set Format:
-(void) Setdateformat: (NSString *) string;
Print As String information:
-(NSString *) Stringfromdate: (NSDate *) date;
To set up a format template:
+ (NSString *) Dateformatfromtemplate: (NSString *) tmplate options: (Nsuinteger) opts locale: (Nslocale *) locale Ns_ AVAILABLE (10_6, 4_0);
For a dateformat format, here are a few examples:
NSDate *date = [NSDate Date];
NSDateFormatter *DF = [[NSDateFormatter alloc] init];
[DF setdateformat:@ "dd/mm/yyyy hh:mm:ss aaa"];
NSLog (@ "curr=%@, dateformate=%@", [DF Stringfromdate:date], Df.dateformat);
Change DataFormat can change the output format of time
curr=27/04/2013 03:42:21 PM, dateformate=dd/mm/yyyy hh:mm:ss AAA
Curr=02013-4 Month-27 A.D. 03:45 PM, Dateformate=yyyyy-mmmm-dd GGG hh:mm AAA
Curr=3:45 PM, Greenwich Mean Time +0800, dateformate=k:mm A, Z
curr=03 o ' clock PM, China Standard Time, dateformate=hh ' o ' clock ' a,zzzz
Curr=3:46 PM, dateformate=h:mm a
Curr= Saturday, April, ' dateformate=eee, MMM D, ' yy
curr=2013:04:27 A.D. at 15:47:11 Greenwich Mean Time +0800, Dateformate=yyyy:mm:dd G ' at ' HH:mm:ss zzz
This is a few examples of online search, if you want to display the format you want, you also need to write the format to
+ (NSString *) Dateformatfromtemplate: (NSString *) template options: (Nsuinteger) opts locale: (Nslocale *) locale
This method can be used to set up a template for display format, which is convenient for later use.
See here, it is estimated that everyone will think: The format of the settings are there any rules, or how to write it?
I searched with this question, and the result was written in another blog, "The meaning of the time format string"
3. Nsdatecomponents class
This class is not mysterious, is that we are more familiar with the date of the month and minute information, in some of the calculation display is very useful, with the NSDate class is not alone to obtain each element information, you must use this nsdatecomponents class.
4. Nscalendar class
This class is used to describe a particular calendar. It also provides most of the time-based interfaces and links the NSDate class to the Nsdatecomponents class.
These classes would have looked at each other to achieve their respective functions, but the so-called date and time information must be in line with a calendar to express, such as: The same day, the Gregorian and lunar calendar is possible different date, or even different months of different years
So when you say xxxx xx month xx days, the machine must be based on a calendar (the most commonly used of course is the Gregorian calendar), So take a closer look at the two classes of NSDateFormatter and nsdatecomponents, all of which have calendar data members. Among them, nsdatecomponents this class has both NSDate and Nscalendar members.
-(Nsdatecomponents *) components: (Nsuinteger) unitflags fromdate: (nsdate *) Date
According to the flag, go from nsdate to nsdatecomponents so that the corresponding time element can be obtained
-(Nsdatecomponents *) components: (Nsuinteger) unitflags fromdate: (nsdate *) startingdate toDate: (nsdate *) resultdate Options: (Nsuinteger) opts
You can get the interval between the start/end time and use nsdatecomponents to represent
-(NSDate *) datebyaddingcomponents: (nsdatecomponents *) comps toDate: (NSDate *) Date options: (Nsuinteger) opts
Returns an NSDate object after adding a nsdatecomponents
-(NSDate *) datefromcomponents: (nsdatecomponents *) comps
Convert from Nsdatecomponets object to NSDate object
The specific use reflects
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;
}
Class and use of processing time in iOS