Use of time classes such as iOS NSDate and iosnsdate
I. NSDate
// IOS time // current time, default 0 time zone NSDate * date = [NSDate date]; NSLog (@ "current time date % @", date ); // NSDateFormatter is used to set the NSDate format NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; // set it to the system time zone formatter. timeZone = [NSTimeZone systemTimeZone]; // used to set the output format of NSDate formatter. dateFormat = @ "yyyy-MM-dd HH: mm: ss"; // output NSString * timeStr = [formatter stringFromDate: [NSDate date]; NSLog (@ "output format of local time NSDate % @", timeStr );
In addition, the following methods can be used to compare dates:
// Returns YES-(BOOL) is1_todate :( NSDate *) otherDate when compared with otherDate. // returns the earlier date-(NSDate *) when compared with anotherDate *) earlierDate :( NSDate *) anotherDate; // compare with anotherDate, return the date later-(NSDate *) laterDate :( NSDate *) anotherDate;
Ii. NSDateFormatter
// Convert a string to a date object. NSString * dateStrs = @ "2020-01-01-18:10:00"; NSDateFormatter * formatterTime = [NSDateFormatter new]; formatterTime. dateFormat = @ "yyyy-MM-dd-HH: mm: ss"; formatterTime. timeZone = [NSTimeZone timeZoneWithAbbreviation: @ "GMT"]; NSDate * dateTime = [formatterTime dateFromString: dateStrs]; NSLog (@ "Convert string to date object date % @", dateTime );
NSDateFormatter format parameters are as follows: G: the age of the public, for example, AD yy: the last two digits of yyyy: full year MM: Month, displayed as 1-12 MMM: month, display is short for the English month, such as Jan MMMM: Month, display as the full name of the English month, such as Janualy dd: Day, 2 digits, such as 02 d: Day, 1-2 digits display, for example, 2 EEE: abbreviated to the day of the week, such as Sun EEEE: the day of the week, such as Sunday aa: Upper afternoon, AM/pm h: hour, 24-hour, 0-23 K: hour, in 12-hour format, 0-11 m: minute, 1-2mm: minute, 2-bit s: Second, 1-2 ss: Second, 2-bit S: millisecond common date structure: yyyy-MM-dd HH: mm: ss. SSS yyyy-MM-dd HH: mm: ss yyyy-MM-dd MM dd yyyy
Iii. NSDate and timestamp Conversion
// NSDate is converted to the timestamp NSString * timeSp = [NSString stringWithFormat: @ "% ld", (long) [[NSDate date] timeIntervalSince1970]; NSLog (@ "timeSp: % @ ", timeSp); // converts the timestamp to NSDate * currentTime = [NSDate dateWithTimeIntervalSince1970: [timeSp intValue]; NSLog (@" currentTime: % @", currentTime );
Iv. NSCalendar
// Obtain each part of the NSDate object, such as year, month, and day. // 1. create one current Calendar Object. // function: obtain each part of a date object. NSCalendar * calendar = [NSCalendar currentCalendar]; // 2. specify the Calendar Object to retrieve the part of the date object. NSDateComponents * comp = [calendar components: NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday fromDate: date]; // 3. obtain each part of the specified date through NSDateComponents. NSLog (@ "% lu", comp. year); NSLog (@ "% lu", comp. month); NSLog (@ "% lu", comp. day); NSLog (@ "% lu", comp. weekday );