1. Resolving the time to get NSDate is incorrect
NSDate *nowdate = [NSDate Date];
Nstimezone *localtimezone = [Nstimezone localtimezone];
Nsinteger ti = [Localtimezone secondsfromgmtfordate:nowdate];
NSDate *newdate = [Nowdate Datebyaddingtimeinterval:ti];
NSLog (@ "World unification Time:%@ local time:%@", Nowdate, newdate);
1.获取当前时间的年月日时分秒
Nscalendar *calendar = [Nscalendar Currentcalendar];
Nscalendarunit unit = Nscalendarunityear | Nscalendarunitmonth | Nscalendarunitday | Nscalendarunithour | Nscalendarunitminute | Nscalendarunitsecond;
nsdatecomponents *components = [Calendar components:unit fromdate:[nsdate Date]];
NSLog (@ "Year:%ld months:%ld days:%ld:%ld:%ld seconds:%ld", (Long) components.year, (Long) components.month, (Long) Components.day, ( Long) Components.hour, (Long) Components.minute, (long) components.second);
2.比较两个时间之间的差值
NSDate *date1 = [NSDate Date]; The current date
NSString *datestr = @ "2015-06-29 07:05:26 +0000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
Formatter.dateformat = @ "Yyyy-mm-dd HH:mm:ss Z";
NSDate *date2 = [Formatter datefromstring:datestr]; The specified date
Nscalendar *calendar = [Nscalendar Currentcalendar];
Nscalendarunit unit = Nscalendarunityear | Nscalendarunitmonth | Nscalendarunitday | Nscalendarunithour | Nscalendarunitminute | Nscalendarunitsecond;
nsdatecomponents *components = [Calendar components:unit fromdate:date1 todate:date2 options:0];
NSLog (@ "Difference year:%ld months:%ld days:%ld:%ld:%ld seconds:%ld", (Long) components.year, (Long) components.month, (Long) Components.day, ( Long) Components.hour, (Long) Components.minute, (long) components.second);
Basic use of time-related Nscalendar and NSDate classes