15th-day notes (operations on OC NSDate) and iosnsdate for IOS

Source: Internet
Author: User

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

 

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.