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
- // Obtain the current date
-
- NSDate * date = [NSDate date];
-
-
- // Print the result: Current Time: date = 09:00:04 + 0000
-
- NSLog (@ "current time date = % @", date );
-
-
- // 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.
-
- Date = [[NSDate alloc] initWithTimeInterval: 60 sinceDate: [NSDate date];
-
-
- // Print the result: the 60 s time after the current time date = 09:01:04 + 0000
-
- 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:
- NSTimeZone * zone = [NSTimeZone systemTimeZone];
-
-
- NSInteger interval = [zone secondsFromGMTForDate: date];
-
-
- NSDate * localDate = [date dateByAddingTimeInterval: interval];
-
-
- // Print the result correctly. Current Time: localDate = 17:01:04 + 0000
-
- NSLog (@ "correct current time localDate = % @", localDate );
Ii. NSDate and NSString Conversion
- /* ---- NSDate and NSString ----*/
-
- NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
-
-
- // Set the date format
-
- [DateFormatter setDateFormat: @ "YYYY/mm/dd time hh: mm: ss"];
-
-
- NSString * dateString = [dateFormatter stringFromDate: [NSDate date];
-
-
- // Print the result: dateString = 2013/10/16 05:15:43
-
- NSLog (@ "dateString = % @", dateString );
-
-
-
- // Set the date format
-
- [DateFormatter setDateFormat: @ "YYYY-MM-dd"];
-
-
- NSString * year = [dateFormatter stringFromDate: [NSDate date];
-
-
- // Print the result: year, month, day, year =
-
- NSLog (@ "year month day year = % @", year );
-
-
- // Set the time format
-
- [DateFormatter setDateFormat: @ "hh: mm: ss"];
-
-
- NSString * time = [dateFormatter stringFromDate: [NSDate date];
-
-
- // Print the result. time = 05:15:43.
-
- NSLog (@ "time = % @", time );
3. Comparison of dates
- /* ---- Comparison of date and time ----*/
-
- // Current time
-
- NSDate * currentDate = [NSDate date];
-
-
- // One hour later than the current time
-
- NSDate * laterDate = [[NSDate alloc] initWithTimeInterval: 60*60 sinceDate: [NSDate date];
-
-
- // One hour earlier than the current time
-
- NSDate * earlierDate = [[NSDate alloc] initWithTimeInterval:-60*60 sinceDate: [NSDate date];
-
-
- // Compare the time delay
-
- If ([currentDate laterDate: laterDate]) {
-
- // Print the result: current-2013-08-16 09:25:54 + 0000 is later than later-2013-08-16 10:25:54 + 0000
-
- NSLog (@ "current-% @ later than later-% @", currentDate, laterDate );
-
- }
-
-
- // Compare the earlier time
-
- If ([currentDate earlierDate: earlierDate]) {
-
- // Print the result: current-2013-08-16 09:25:54 + 0000 compared to earlier-2013-08-16 08:25:54 + 0000
-
- NSLog (@ "current-% @ earlier than earlier-% @", currentDate, earlierDate );
-
- }
-
-
- If ([currentDate compare: earlierDate] = NSOrderedDescending ){
-
- // Print the result
-
- NSLog (@ "current late ");
-
- }
-
- If ([currentDate compare: currentDate] = NSOrderedSame ){
-
- // Print the result
-
- NSLog (@ "equal time ");
-
- }
-
- If ([currentDate compare: laterDate] = NSOrderedAscending ){
-
- // Print the result
-
- NSLog (@ "current early ");
-
- }