Use Objective-C to obtain the year, month, day, hour, minute, and second of the NSDate object & amp; NSCalender

Source: Internet
Author: User

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 );


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.