Use Objective-C to obtain the year, month, day, hour, minute, and second of the NSDate object & amp; NSCalender
/*
Most of the methods on the Internet to obtain NSDate, month, day, hour, minute, and second are marked by Apple as unavailable on mac OS 10.10.
After checking for a long time, I finally found a method recommended by Apple. I will share the following content with you:
*/
/* The current time object [dateNow] */NSDate * dateNow = [NSDate date];/* calendar class [calendar] Note: You must use [NSCalendar currentCalendar] to initialize it, if [[NSCalendar alloc] init] is used for initialization, the obtained time is random. */NSCalendar * calendar = [NSCalendar currentCalendar];/* variable */NSInteger year; // year NSInteger month; // month NSInteger day; // day NSInteger hour; // NSInteger minute; // NSInteger second; // second NSInteger nanosecond; // 10 ^-9 seconds/* use the [calendar ar] object to obtain [hour] [minute] [second] [nanosecond] from [dateNow]. Note: '&' is an address character. When the "Address" of the variable is passed over, the function will write the corresponding value to this "Address", and the corresponding value will be saved in the variable. */[calendar getHour: & hour minute: & minute second: & second nanosecond: & nanosecond fromDate: dateNow]; /* use the [calendar] object to obtain [year], [month], and [day] from [dateNow]. note: [Era] indicates a. D. The current date is 1. since there is no document describing the meaning of this value, the usage requirement is not high. */[calendar ge1_: nil year: & year month: & month day: & day fromDate: dateNow];/* print the obtained values. Note: '% d' is a 32-bit integer. '% ld' is a 64-bit integer. '% 2ld' is a 64-bit integer. If there are less than two digits, add a space on the left. '% 02ld' is a 64-bit integer. If there are less than two digits, add '0' to the left '. output result: 13:44:34. 582 MyCake [3020: 106505] 13:44:34. 582 result Description: Compare the system output time with your time. in addition, the millimeter numbers are the same, indicating that the writing efficiency is very high. */NSLog (@ "% 04ld-% 02ld-% 02ld % 02ld: % 02ld: % 02ld. % 03ld ", year, month, day, hour, minute, second, nanosecond/1000000 );