Objective-c Study 10-nsdate and NSDateFormatter

Source: Internet
Author: User
Tags time zones

 NSDate

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

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

2015-12-04 19:08:00.624 ocnsdate[2955:309612] date = 2015-12-04 11:08:00 +0000

Printed display is galini time-month-day time: minutes: seconds + Time zone

1. Get a method to create a distance from the current time interval datewithtimeintervalsincenow: (nstimeinterval)

The essence of (Nstimeinterval) is a double data type

    NSDate *date1 = [nsdate datewithtimeintervalsincenow:8*+]; // 8 hours time difference with Galini, get Beijing time     NSLog (@ "date1 =%@", date1);

2015-12-04 19:08:00.625 ocnsdate[2955:309612] Date1 = 2015-12-04 19:08:00 +0000

 

 Exercise 1: Get the current point in time tomorrow nsdate*nextday = [NSDate datewithtimeintervalsincenow: -* -* -+8* -* -]; Exercise 2: Get next year's point of timeNSDate*nextyear = [NSDate datewithtimeintervalsincenow:366* -* -* -+8* -* -]; NSLog (@"%@", nextyear);

2. Calculates the time interval for a given time and the current point in time . Timeintervalsincenow

Nstimeinterval interval = Nextday.timeintervalsincenow;

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

 3. Calculate the time interval for two points of time timeintervalsincedate:

Nstimeinterval Interval2 = [date timeintervalsincedate:nextyear];

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

   Timestamp concept: A time interval from 1970.1.1, which is in seconds, called a timestamp

  the method of time stamp: timeIntervalSince1970

Nstimeinterval INTERVAL3 = [date timeIntervalSince1970];

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

  Convert a timestamp to a time object dateWithTimeIntervalSince1970:

NSDate *date2 = [NSDate datewithtimeintervalsince1970:3600];

NSLog (@ "%@", date2);

  Get Beijing Time datebyaddingtimeinterval:

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

NSLog (@ "%@", date3);

  Exercise 3: A difference between the current time and a fixed time, if the difference in 60 seconds, then the output "just", if the time difference in 60-3,600 seconds, then the output in "xx minutes ago", if within the 3600~24*3600, then the output in "xx hours ago", if the 24* 3,600 seconds to output fixed time

 fixed time nsdate  *pastdate = [NSDate datewithtimeintervalsincenow:-370]; NSLog (@"%@", pastdate);
  Current Time NSDate*nowdate =[NSDate Date]; the difference between the fixed time and the current time Nstimeinterval Interval4=[Nowdate timeintervalsincedate:pastdate]; NSLog (@"Time difference is%.2f seconds", INTERVAL4);
if(Interval4 <= -) {NSLog (@"just"); }Else if(Interval4 <=3600) {NSLog (@"%.f minutes ago", Interval4/ -);
}Else if(Interval4 <= -*3600) {NSLog (@"%.f hours ago", Interval4/3600); }Else if(Interval4 > -*3600) {NSLog (@"%@", pastdate); }

NSDateFormatter

NSDateFormatter date format class, inherited from Nsformatter, is the primary function of converting NSDate objects to a format and outputting them as strings

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

  1. Set the letter used in the date format: Y for the year, M for the month, and D for the day H for the M to represent the seconds

    [Formartter Setdateformat:@ "yyyy mm month dd Day hh mm min ss sec "]    ; *date4 = [nsdate datewithtimeintervalsincenow:0];

  2. Convert the time object to a formatted format stringfromdate:

When formatted, the system automatically adds time intervals from 0 time zones.

    NSString *datestring = [Formartter stringfromdate:date4];    NSLog (@ "%@", datestring);

Exercise: Output DATE4 as @ "2015 11:43 20 sec"

  Create a Time Format class object init method creation

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

  Specify the output format setdateformat:@ "Specific time format"

[MyFormatter setdateformat:@] yyyy year mm month DD number hh point mm min SS second "];

  Receive the converted time stringfromdate with a string:

NSString *datestring1 = [MyFormatter stringfromdate:date4];

NSLog (@ "%@", dateString1);

  Convert a time string to a NSDate object

For example:@"November 24, 2015 11:10 10 seconds"NSDateFormatter*formatter3 =[[NSDateFormatter alloc] init]; [Formatter3 Setdateformat:@"yyyy mm month dd number hh mm min ss sec"];    prepare the time string NSString*datestring3 =@"November 24, 2015 11:10 10 seconds";    use time format objects to format time objects with time strings NSDate*date5 =[Formatter3 Datefromstring:datestring3];
  the time to turn around will be back to 0 time zones. NSLog (@"%@", Date5); you need to manually add 8 hours if you want to NSDate*date6 = [Date5 datebyaddingtimeinterval:8* -* -]; NSLog (@"%@", DATE6);

Objective-c Learning article 10-nsdate and NSDateFormatter

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.