Objective-C: 10-NSDate and NSDateFormatter, objective-cnsdate

Source: Internet
Author: User
Tags date1

Objective-C: 10-NSDate and NSDateFormatter, objective-cnsdate

NSDate

NSDate time class, inherited from NSObject, whose object represents a time point

    NSDate *date = [NSDate date];    NSLog(@"date = %@", date);

19:08:00. 624 OCNSDate [2955: 309612] date = 11:08:00 + 0000

Print the displayedGreenwich time-month-day hour: minute: Second + Time Zone

1. Get a method to create a time point from the current time interval.DateWithTimeIntervalSinceNow :( NSTimeInterval)

(NSTimeInterval) is essentially a double data type.

NSDate * date1 = [NSDate dateWithTimeIntervalSinceNow: 8*60*60]; // Add the time difference of 8 hours to the GMT time, obtain NSLog (@ "date1 =%@", date1) at Beijing time );

2015-4 4 19:08:00. 625OCNSDate [2955: 309612] date1 =19:08:00 + 0000

 

Exercise 1: Get the current time tomorrow NSDate * nextDay = [NSDate dateWithTimeIntervalSinceNow: 24*60*60 + 8*60*60]; Exercise 2: obtain the NSDate * nextYear = [NSDate dateWithTimeIntervalSinceNow: 366*24*60*60 + 8*60*60]; NSLog (@ "% @", nextYear) next year );

 

2. Calculate the time interval between the specified time and the current time point. timeIntervalSinceNow

NSTimeInterval interval = nextDay. timeIntervalSinceNow;

NSLog (@ "%. 2f", interval );

 3. Calculate the time interval between the two time points timeIntervalSinceDate:

NSTimeInterval interval2 = [date timeIntervalSinceDate: nextYear];

NSLog (@ "%. 2f", interval2 );

  TimestampConcept: The time interval between a time point and 1970.1.1. This time is measured in seconds and is called a timestamp.

  Timestamp method: TimeIntervalSince1970

NSTimeInterval interval3 = [date timeIntervalSince1970];

NSLog (@ "%. 2f", interval3 );

  Converts a timestamp to a time object. DateWithTimeIntervalSince1970:

NSDate * date2 = [NSDate dateWithTimeIntervalSince1970: 3600];

NSLog (@ "% @", date2 );

  Get Beijing TimeDateByAddingTimeInterval:

NSDate * date3 = [date dateByAddingTimeInterval: 8*60*60];

NSLog (@ "% @", date3 );

  Exercise 3: The difference between the current time and a fixed time. If the difference is within 60 seconds, the output is "just". If the difference is between 60 and 60 ~ 3600 seconds, the output is before "xx minutes ~ Within 24*3600, the output time is before "xx hours". If the output time is beyond 24*3600 seconds

  Fixed timeNSDate * pastDate = [NSDate dateWithTimeIntervalSinceNow:-370]; NSLog (@ "% @", pastDate );
  Current TimeNSDate * nowDate = [NSDate date];Difference between the fixed time and the current timeNSTimeInterval interval4 = [nowDate timeIntervalSinceDate: pastDate]; NSLog (@ "Time Difference: %. 2f seconds", interval4 );
If (interval4 <= 60) {NSLog (@ "");} else if (interval4 <= 3600) {NSLog (@ "%. f Minutes Ago ", interval4/60 );
} Else if (interval4 <= 24*3600) {NSLog (@ "%. f hours ago ", interval4/3600);} else if (interval4> 24*3600) {NSLog (@" % @ ", pastDate );}

 

 

NSDateFormatter

NSDateFormatter Date Format class, inherited from NSFormatter, mainly used to convert an NSDate object to a certain format and then output it as a string

NSDateFormatter * formartter = [[NSDateFormatter alloc] init];

  1. Set the letter used in the Date Format: y represents the year, M represents the month, d represents the day H Represents the hour m represents the minute, s represents the second

[Formartter setDateFormat: @ "MM: dd, yyyy, HH, mm: ss seconds"]; NSDate * date4 = [NSDate dateWithTimeIntervalSinceNow: 0];

 2. Convert the time object to the set format stringFromDate:

During formatting, the system automatically adds the time interval from the zero time zone.

    NSString *dateString = [formartter stringFromDate:date4];    NSLog(@"%@", dateString);

Exercise: Output date4 in the form of @ "2015"

  Create a time format Class Object init Method

NSDateFormatter * myFormatter = [[NSDateFormatter alloc] init];

  SetDateFormat: @ "specific time format"

[MyFormatter setDateFormat: @ "yyyy Year MM Month dd No. HH point mm minute ss seconds"];

  Use a string to receive the converted time stringFromDate:

NSString * dateString1 = [myFormatter stringFromDate: date4];

NSLog (@ "% @", dateString1 );

  Converts a time string to an NSDate object.

For example: @ "November 24, 2015" NSDateFormatter * formatter3 = [[NSDateFormatter alloc] init]; [formatter3 setDateFormat: @ "MM dd, yyyy, HH, mm, ss seconds"];Preparation Time stringNSString * dateString3 = @ "November 24, 2015 ";Format a time object using a time stringNSDate * date5 = [formatter3 dateFromString: dateString3];
  The transferred time is returned to the zero time zone.NSLog (@ "% @", date5 );If you want to get Beijing time, you need to manually add 8 hoursNSDate * date6 = [date5 dateByAddingTimeInterval: 8*60*60]; NSLog (@ "% @", date6 );
 

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.