Objective-C study note _ NSDate and NSDateFormatter

Source: Internet
Author: User

Objective-C study note _ NSDate and NSDateFormatter
I. How to Use NSDate?

In iOS development, NSDate is used in many scenarios, such as computing the time displayed in a chat program (several minutes ago, several hours ago, several days ago );Timestamp (calculate the current time from January 1, 1970? 1? In seconds ).

NSDate is the basic class used in Cocoa to process dates and times? The given time point (including the date, time, and time zone ). Use the + date method to obtain the current time. For example:NSDate *nowDate = [NSDate date];Note:NSLog(@“%@”, nowDate); ? Which time zone do you use? The time zone is always 0.

NSTimeinterval

NSTimeInterval(Double type), used for tables?Time Interval in seconds. Available-initWithTimeIntervalSinceNow:Method passed in? NSTimeInterval parameters, create an NSDate object.

For example:NSDate *tomorrowDate = [[NSDate alloc] initWithTimeIntervalSinceNow: 24 * 60 * 60];
NSDate *yesterdayDate = [[NSDate alloc] initWithTimeIntervalSinceNow: -1 * 24 * 60 * 60];Take the interval of two time objects:NSTimeinterval = [tomorrowDate timeIntervalSinceDate: yesterdayDate];

The specific implementation code is as follows:

# Pragma mark ** NSDate date type NSDate * date = [NSDate date];/* 0 time zone */NSLog (@%@, date ); /* 0 Time zone + seconds = Current Time zone * // * obtain the current time zone */NSTimeZone * zone = [NSTimeZone systemTimeZone]; NSLog (@ % @, zone); NSTimeZone * zone2 = [NSTimeZone localTimeZone]; NSLog (@ % @, zone2);/* obtain the number of seconds from the 0 time zone */NSInteger seconds = [zone secondsFromGMTForDate: date];/* current time */NSDate * localDate = [NSDate dateWithTimeIntervalSinceNow: seconds]; NSLog (@ localDate: % @, localDate); NSDate * local2 = [NSDate dateWithTimeIntervalSinceNow: 8*3600]; NSLog (@ local2: % @, local2);/* tomorrow time */NSDate * tomorrow = [NSDate dateWithTimeIntervalSinceNow: 8*3600 + 24*3600]; NSLog (@ tomorrow: % @, tomorrow);/* yesterday's time */NSDate * yesterday = [NSDate dateWithTimeIntervalSinceNow: 8*3600-24*3600]; NSLog (@ yesterday: % @, yesterday); # pragma mark ** NSTimeInterval time interval class NSTimeInterval timeInterval = [tomorrow timeIntervalSinceDate: yesterday]; NSLog (@ timeInterval: % lf, timeInterval ); NSTimeInterval timeInterval2 = [localDate timeIntervalSince1970]; NSLog (@ timeInterval2: % lf, timeInterval2);/* get the time of the current time zone quickly */NSDate * dateLocal = [NSDate hour: [NSTimeZone localTimeZone]. secondsFromGMT]; NSLog (@ dateLocal: % @, dateLocal );
2. How to Use NSDateFormatter?

NSDateFormatterIs in iOSDate Format, Function isMutual conversion between NSString and NSDate. CommonTime Format StringInclude:Y, month M, day d, hour H (in the 24-hour format), hour h (in the 12-hour format), and am | pm morning | afternoon, m minutes, And seconds. The specified date format:NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@yyyy-MM-dd HH:mm:ss];

NSDate to NSString

Convert date to string:

NSDateFormatter*formatter = [[NSDateFormatter alloc] init];[formatter setDateFormat:@yyyy-MM-dd hh:mm:ss];NSString *dateString = [formatter stringFromDate: [NSDate date]];
NSString to NSDate

Is the time string converted to the corresponding? Period:

NSDateFormatter * formatter = [[NSDateFormatter alloc] init];[formatter setDateFormat:@yyyy-MM-dd HH:mm:ss];NSString *dateStr = @”2008-08-08 20:08:08”;NSDate *date = [formatter dateFromString:dateStr];

The specific implementation code is as follows:

# Pragma mark ** NSDateFormatter/* y * M month * d day * h hour (12), H (24) * m minutes * s seconds * a shows the * z time zone * eee week * G ad * // * NSDate to string * // * method 1, string format Method */NSString * string1 = [NSString stringWithFormat: @ % @, dateLocal]; NSLog (@ string1: % @, string1);/* method 2, NSDateFormatter format * // * Step: 1. create a new format * 2. convert by format */NSDateFormatter * format1 = [[NSDateFormatter alloc] init]; [format1 setAMSymbol: @ AM]; [Format1 setPMSymbol: @ PM]; [format1 setDateFormat: @ yyyy MM dd HH: mm: ss]; NSString * string2 = [format1 stringFromDate: date]; NSLog (@ string2: % @, string2);/* NSDateFormat is automatically converted to the current system time zone * // * string to NSDate */NSString * string3 = @ 19:44:16; NSDate * date11 = [format1 dateFromString: string3]; NSLog (@ date11: % @, date11);/* exercise: Define an NSDateFormatter, set the appropriate format * Add the string @ "2014 05? 01, and 18 seconds "are converted to an NSDate object. */NSDateFormatter * exFormat = [[NSDateFormatter alloc] init]; [exFormat setDateFormat: @ yyyy MM dd HH: mm: ss seconds]; NSString * timeStr = @, January 1, May 01, 2014; NSDate * dateStr = [[exFormat dateFromString: timeStr] dateByAddingTimeInterval: [NSTimeZone localTimeZone]. secondsFromGMT]; NSLog (@ timeStrDate: % @, dateStr );

 

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.