IOS development-NSDateFormatter

Source: Internet
Author: User

IOS development-NSDateFormatter

The formatting parameters are as follows:

G: The age of the public, for example, AD yy: the last two digits of the year yyyy: full year MM: Month, displayed as 1-12 Mmm: Month, displayed as short as the English month, such as JanMMMM: month, display is the full name of the English month, such as Janualydd: Day, 2 digits, such as 02d: Day, 1-2 digits, such as 2EEE: abbreviated day of the week, such as SunEEEE: full day of the week, for example, Sundayaa: Upper afternoon, AM/PMH: hour, 24-hour, 0-23 K: hour, 12-hour, 0-11 m: minute, 1-2mm: minute, 2-bit s: Second, 1-2-bit ss: Second, 2-bit S: millisecond

Common date structure:

yyyy-MM-dd HH:mm:ss.SSSyyyy-MM-dd HH:mm:ssyyyy-MM-ddMM dd yyyy

Sample Code:

-(Void) loadBaseUI {// instantiate NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat: @ yyyy-MM-dd HH: mm: ss]; // date-> string (converted to date in the specified format) NSDate * currentDate = [NSDate date]; NSString * dateString = [dateFormatter stringFromDate: currentDate]; NSLog (@ % @, dateString); // string-> date (the format set by dateFormatter must be consistent with the string format, otherwise null is returned) NSDateFormatter * dateFormatter2 = [[NSDateFormatter alloc] init]; [dateFormatter2 setDateFormat: @ yyyy-MM-dd HH: mm]; NSString * string = @ August 28, 2015; NSDate * target = [dateFormatter2 dateFromString: string]; NSLog (@ % @, target); // output // 14:10:11. 771 NSDateFormatter [2950: 109663] 14:10:11 // 14:10:11. 773 NSDateFormatter [2950: 109663] 05:55:00 + 0000}

// Obtain the specified time

-(NSString *) GetTomorrowDay :( NSDate *) aDate {// km calendar NSCalendar * gregorian = [[NSCalendar alloc] region: NSCalendarIdentifierGregorian]; NSDateComponents * components = [gregorcomponian: california | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate: aDate]; [components setDay :( [components day] + 1)]; // obtain the NSDate of the next day * Principal = [gregorian dateFromComponents: components]; NSDateFormatter * dateday = [[NSDateFormatter alloc] init]; [dateday setDateFormat: @ yyyy-MM-dd]; return [dateday stringFromDate: beginningOfWeek];}

 

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.