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];}