Get current time
NSDate *now = [NSDate date]; NSLog (@ "Now date was:%@", now); Nscalendar *calendar = [Nscalendar Currentcalendar]; Nsuinteger unitflags = Nsyearcalendarunit | Nsmonthcalendarunit | Nsdaycalendarunit | Nshourcalendarunit | Nsminutecalendarunit | Nssecondcalendarunit; nsdatecomponents *datecomponent = [Calendar components:unitflags fromdate:now]; int year = [datecomponent year]; int month = [datecomponent month]; int day = [Datecomponent day]; int hour = [Datecomponent hour]; int minute = [datecomponent minute]; int second = [datecomponent second]; NSLog (@ "Year was:%d", year); NSLog (@ "Month is:%d", month); NSLog (@ "Day was:%d", day); NSLog (@ "Hour is:%d", hour); NSLog (@ "Minute is:%d", minute); NSLog (@ "Second is:%d", second);
Calculate age
The first is only the age of the year difference
Calculate Age NSString *birth = @ "1993-10-30"; NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init]; [Dateformatter setdateformat:@ "Yyyy-mm-dd"]; Birthday NSDate *birthday = [Dateformatter Datefromstring:birth]; Current time nsstring *currentdatestr = [dateformatter stringfromdate:[nsdate Date]]; NSDate *currentdate = [Dateformatter datefromstring:currentdatestr]; NSLog (@ "currentdate%@ birthDay%@", Currentdatestr,birth); Nstimeinterval time=[currentdate Timeintervalsincedate:birthday]; int age = ((int) time)/(3600*24*365); NSLog (@ ' year%d ', age);
The other one draws details to the age of the day
Nscalendar *calendar = [Nscalendar currentcalendar];//defines a Nscalendar object nsdate *nowdate = [NSDate Date]; NSString *birth = @ "1900-10-30"; NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init]; [Dateformatter setdateformat:@ "Yyyy-mm-dd"]; Birthday NSDate *birthday = [Dateformatter Datefromstring:birth]; Used to get the detailed time difference unsigned int unitflags = Nsyearcalendarunit | Nsmonthcalendarunit | Nsdaycalendarunit | Nshourcalendarunit | Nsminutecalendarunit | Nssecondcalendarunit; nsdatecomponents *date = [Calendar components:unitflags fromdate:birthday todate:nowdate options:0]; if ([date year] >0) {NSLog (@ "%@", [NSString stringWithFormat: (@ "%ld years%ld months%ld days"), (long) [date year], (long) [Date m Onth], (long) [date Day]]); The Else if ([date month] >0) {NSLog (@ "%@", [NSString stringWithFormat: (@ "%ld month%ld Days"), (long) [date month], (long ) [Date Day]]; } else if ([date day]>0) {NSLog (@ "%@", [NSString stringWithFormat: (@ "%ld days"),(long) [Date Day]]); } else {NSLog (@ "0 days"); }
string, date, int conversion
This entry: http://blog.csdn.net/tt5267621/article/details/7720434
1. String conversion to date
nsdateformatter* DateFormat = [[NSDateFormatter alloc] init];//instantiates a NSDateFormatter object
[DateFormat setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];//set the time format, where you can set the format you need
NSDate *date =[dateformat datefromstring:@ "1980-01-01 00:00:01"];
2. Convert Date to String
nsdateformatter* DateFormat = [[NSDateFormatter alloc] init];//instantiates a NSDateFormatter object
[DateFormat setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];//set the time format, where you can set the format you need
NSString *currentdatestr = [DateFormat stringfromdate:[nsdate Date]];
3. String to int
Convert NSString to int
NSString *anumberstring = @ "123";
int i = [anumberstring intvalue];
4, int to string
Convert int to NSString
int anumber = 123;
NSString *astring = [NSString stringwithformat:@ "%d", anumber];
IOS gets the current time and calculates the Age (difference)