Objective-C learning NSDate simple instructions for use

Source: Internet
Author: User

The basic object-c syntax NSDate is used to learn about NSDate settings, get the current time, the time after the current time plus or minus seconds, date comparison, and date conversion to NSString.

XCode4.6.3, OS X SDK 10.8; NSDate date operation, get current date, date comparison, date format, date calculation, Time Difference Solution, etc.

I. NSDate Initialization

 
 
  1. // Obtain the current date
  2.  
  3. NSDate * date = [NSDate date];
  4.  
  5.  
  6. // Print the result: Current Time: date = 09:00:04 + 0000
  7.  
  8. NSLog (@ "current time date = % @", date );
  9.  
  10.  
  11. // Obtain the date from the start of a date or the next time. Here 60 stands for 60 seconds. If you need to obtain the previous date, change 60 to-60.
  12.  
  13. Date = [[NSDate alloc] initWithTimeInterval: 60 sinceDate: [NSDate date];
  14.  
  15.  
  16. // Print the result: the 60 s time after the current time date = 09:01:04 + 0000
  17.  
  18. NSLog (@ "date of 60 s after the current time =%@", date );

PS: the test time is five o'clock P.M., but the current time is nine o'clock A.M.. The difference is 8 hours, which is a time zone problem.

Solution:

 
 
  1. NSTimeZone * zone = [NSTimeZone systemTimeZone];
  2.  
  3.  
  4. NSInteger interval = [zone secondsFromGMTForDate: date];
  5.  
  6.  
  7. NSDate * localDate = [date dateByAddingTimeInterval: interval];
  8.  
  9.  
  10. // Print the result correctly. Current Time: localDate = 17:01:04 + 0000
  11.  
  12. NSLog (@ "correct current time localDate = % @", localDate );

Ii. NSDate and NSString Conversion

 
 
  1. /* ---- NSDate and NSString ----*/
  2.  
  3. NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
  4.  
  5.  
  6. // Set the date format
  7.  
  8. [DateFormatter setDateFormat: @ "YYYY/mm/dd time hh: mm: ss"];
  9.  
  10.  
  11. NSString * dateString = [dateFormatter stringFromDate: [NSDate date];
  12.  
  13.  
  14. // Print the result: dateString = 2013/10/16 05:15:43
  15.  
  16. NSLog (@ "dateString = % @", dateString );
  17.  
  18.  
  19.  
  20. // Set the date format
  21.  
  22. [DateFormatter setDateFormat: @ "YYYY-MM-dd"];
  23.  
  24.  
  25. NSString * year = [dateFormatter stringFromDate: [NSDate date];
  26.  
  27.  
  28. // Print the result: year, month, day, year =
  29.  
  30. NSLog (@ "year month day year = % @", year );
  31.  
  32.  
  33. // Set the time format
  34.  
  35. [DateFormatter setDateFormat: @ "hh: mm: ss"];
  36.  
  37.  
  38. NSString * time = [dateFormatter stringFromDate: [NSDate date];
  39.  
  40.  
  41. // Print the result. time = 05:15:43.
  42.  
  43. NSLog (@ "time = % @", time );

3. Comparison of dates

 
 
  1. /* ---- Comparison of date and time ----*/
  2.  
  3. // Current time
  4.  
  5. NSDate * currentDate = [NSDate date];
  6.  
  7.  
  8. // One hour later than the current time
  9.  
  10. NSDate * laterDate = [[NSDate alloc] initWithTimeInterval: 60*60 sinceDate: [NSDate date];
  11.  
  12.  
  13. // One hour earlier than the current time
  14.  
  15. NSDate * earlierDate = [[NSDate alloc] initWithTimeInterval:-60*60 sinceDate: [NSDate date];
  16.  
  17.  
  18. // Compare the time delay
  19.  
  20. If ([currentDate laterDate: laterDate]) {
  21.  
  22. // Print the result: current-2013-08-16 09:25:54 + 0000 is later than later-2013-08-16 10:25:54 + 0000
  23.  
  24. NSLog (@ "current-% @ later than later-% @", currentDate, laterDate );
  25.  
  26. }
  27.  
  28.  
  29. // Compare the earlier time
  30.  
  31. If ([currentDate earlierDate: earlierDate]) {
  32.  
  33. // Print the result: current-2013-08-16 09:25:54 + 0000 compared to earlier-2013-08-16 08:25:54 + 0000
  34.  
  35. NSLog (@ "current-% @ earlier than earlier-% @", currentDate, earlierDate );
  36.  
  37. }
  38.  
  39.  
  40. If ([currentDate compare: earlierDate] = NSOrderedDescending ){
  41.  
  42. // Print the result
  43.  
  44. NSLog (@ "current late ");
  45.  
  46. }
  47.  
  48. If ([currentDate compare: currentDate] = NSOrderedSame ){
  49.  
  50. // Print the result
  51.  
  52. NSLog (@ "equal time ");
  53.  
  54. }
  55.  
  56. If ([currentDate compare: laterDate] = NSOrderedAscending ){
  57.  
  58. // Print the result
  59.  
  60. NSLog (@ "current early ");
  61.  
  62. }

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.