The main time forms of objective-c:nsdate

Source: Internet
Author: User
Tags string back



NSDate: Acquisition and manipulation of time






1. Get the current time


1 // Get the current date
2 NSDate * date = send 


2. Convert date to string and format


1 // date to string
2 NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
3 // YYYY: year MM: month dd: day HH: 24-hour clock hh: 12-hour clock
4 formatter.dateFormat = @ "YYYY-MM-dd HH: mm: ss";
5 NSString * strdate = [formatter stringFromDate: date];
6 NSLog (@ "% @", strdate); 


3. Turn string back to date


1 // String to date
2 NSDate * date2 = [formatter dateFromString: strdate];
3 NSLog (@ "% @", date2); 


4, the string date decomposition into the year, month, day


1 // Decompose date into year, month, and day
2 NSCalendar * calendar = [NSCalendar currentCalendar];
3 NSInteger unitflags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
4 NSDateComponents * components = [calendar components: unitflags fromDate: date];
5 NSLog (@ "year:% ld month:% ld day:% ld", [components year], [components month], [components day]); 


5. Combine the year, month, and day into a date string


 
1 // Combining the year, month, and day into a date
2 [components setYear: 2014];
3 [components setMonth: 9];
4 [components setDay: 1];
5 NSDate * date3 = [calendar dateFromComponents: components];
6 NSLog (@ "% @", date3); 





The main time forms of objective-c:nsdate


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.