15th-day notes (operations on OC NSDate) and iosnsdate for IOS
1 NSDate * newdate = [NSDate date]; 2 // Time Format String 3 NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; 4 [formatter setDateFormat: @ "yyyy-MM-dd HH: mm: ss MMM eee a"]; 5 NSString * strdate = [formatter stringFromDate: newdate]; 6 NSLog (@ "% @", strdate); 7 8 // time string to time type 9 NSDateFormatter * formatter2 = [[NSDateFormatter alloc] init]; 10 NSString * nstr = @ ", January 1, May 01, 2014"; 11 [formatter2 setDateFormat: @ "MM, yyyy, dd, hh, mm, ss, SEC"]; 12 NSDate * date3 = [formatter2 dateFromString: nstr]; 13 // solve the problem of inaccurate time/Second Conversion 14 NSTimeZone * fzone = [NSTimeZone systemTimeZone]; 15 NSInteger finterval = [fzone secondsFromGMTForDate: date3]; 16 NSDate * fDate = [date3 dateByAddingTimeInterval: finterval]; 17 NSLog (@ "% @", fDate ); 18 19 // obtain the system time of the year, month, day, hour, minute, second part 20 NSCalendar * cal = [NSCalendar currentCalendar]; 21 // before Mac 10.10, the system used NSYearCalendarUnit22 unsigned int unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | hour; 23 NSDateComponents * d = [cal components: unitFlags fromDate: newdate]; 24 NSLog (@ "year = % ld", [d year]); 25 NSLog (@ "month = % ld", [d month]); 26 NSLog (@ "day = % ld", [d day]); 27 NSLog (@ "hour = % ld", [d hour]); 28 NSLog (@ "minute = % ld", [d minute]); 29 NSLog (@ "second = % ld", [d second]); 30 31 // convert the input time string to the required time format 32 NSString * dateStr = @ "20:28:40"; 34 NSDateFormatter * format = [[NSDateFormatter alloc] init]; 35 [format setDateFormat: @ "yyyy-MM-dd HH: mm: ss"]; 36 NSDate * fromdate = [format dateFromString: dateStr]; 37 // solve the problem of inaccurate time/minute/Second Conversion 38 NSTimeZone * fromzone = [NSTimeZone systemTimeZone]; 39 NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate]; 40 NSDate * fromDate = [fromdate: frominterval]; 41 NSLog (@ "fromdate = % @", fromDate );