#import <foundation/foundation.h>int Main (int argc, const char * argv[]) {@autoreleasepool {//Get represents the current date, when Between the nsdate nsdate* date1 = [NSDate Date]; NSLog (@ "%@", date1); Gets the date starting from the current time, after one day nsdate* date2 = [[NSDate alloc]initwithtimeintervalsincenow:3600*24]; NSLog (@ "%@", date2); Gets the date starting from the current time, 3 days ago nsdate* date3 = [[NSDate alloc]initwithtimeintervalsincenow: -3600*24*3]; NSLog (@ "%@", date3); Obtained starting from January 1, 1970, 20 years after the date nsdate* date4 = [[NSDate alloc]initwithtimeintervalsince1970:3600*24*366*20]; NSLog (@ "%@", date4); Gets the current locale of the system nslocale* CN = [Nslocale Currentlocale]; Gets the NSDate string NSLog (@ "%@", [date1 DESCRIPTIONWITHLOCALE:CN]) corresponding to the current locale; Get an earlier date between two dates nsdate* earlier = [Date1 Earlierdate:date2]; NSLog (@ "earlier time:%@", earlier); Get a later time between two dates nsdate* later = [daTe1 Laterdate:date2]; NSLog (@ "later time:%@", later); Compare two dates, compare: Method returns Nscomparisonresult enumeration value//The enumeration type contains nsorderdascending,nsordersame,nsordereddescending three values//points Do not represent call compare: the date is the same as before the comparison date, and then switch ([Date1 compare:date3]) {Case Nsorderedascending:nslog ( @ "Date1 in front of Date3"); Break Case Nsorderedsame:nslog (@ "date1 and date3 date equal"); Break Case Nsordereddescending:nslog (@ "Date1 is located after Date3"); }//Get the time difference between two times NSLog (@ "date1 and date3 between%g seconds", [Date1 timeintervalsincedate:date3]); Gets the time difference from the current NSLog (@ "Date2 with the current difference%g seconds", [Date2 Timeintervalsincenow]); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
003-Date and Time (NSDate)