NSDate (16)

Source: Internet
Author: User
Tags sleep function

NSDate is a date and time processing class for OC

When you get a new class, the first thing you think of is the initialization of this class, the initialization method of the NSDate class is:

NSDate *date = [[NSDate alloc]init];//GMT

NSLog (@ "%@", date),//2015-04-2109:54:53.564 oclesson7_nsdate[787:27248] 2015-04-21 01:54:53 +0000//GMT

The initialization method of the NSDate class, the resulting value is Greenwich Mean time, so you need to do some work if you want to get the right times locally.

Nstimezone Get time zone class Localtimezone

To get local time, first take the time zone, at which time the Nstimezone class can help us get the time zone.

Get time in this time zone

1. Get Time Zone

Nstimezone *zone = [Nstimezone localtimezone];

NSLog (@ "%@", Zone),//2015-04-21 09:57:52.731 oclesson7_nsdate[803:28151] Local time zone (Asia/harbin (gmt+8) off Set 28800)

Explain:

1, Localtimezone This method is to get our time zone. So the print is local time Zone (Asia/harbin (gmt+8) offset 28800)

2, offset 28800, print out the East eight and 0 time zone of the number of seconds is 28,800 seconds.

Secondsfromgmt Gets the current time zone's second difference from the 0 time zone (difference)

We use the Nstimezone to get the time zone, also know the difference, this time difference offset of 28,800 seconds, for us to get the local right back to play an important role.

Initwithtimeintervalsincenow by Time difference

After obtaining the above information, we can create local time:

2. Create time, offset

NSDate *date2 = [[NSDate alloc]initwithtimeintervalsincenow:offset];

NSLog (@ "%@", Date2)//2015-04-21 10:01:52.390 oclesson7_nsdate[830:29275] 2015-04-21 10:01:52 +0000

Sleep ()

The sleep function is used to get the program to sleep (pause execution), and sleep (5) indicates that the program sleeps for 5 seconds when executing to this statement.

The use of sleep here is to let the artificial two time to produce a difference, sleep (5) is to let the difference of 5 seconds. But at this point sleep has a bad effect, which is to let the program pause execution. If you get a large time difference, using sleep, the program will be suspended for so long. At this point, OC provides a way to produce a time difference.

First look at the use of sleep:

2. Create time, offset

NSDate *date2 = [[NSDate alloc]initwithtimeintervalsincenow:offset];

NSLog (@ "%@", Date2)//2015-04-21 10:01:52.390 oclesson7_nsdate[830:29275] 2015-04-21 10:01:52 +0000

Get time difference

Sleep (5);//Rest for 5 seconds

New Time

NSDate *date3 = [[NSDate alloc]initwithtimeintervalsincenow:offset];

Ask for two time difference

Nstimeinterval ti = [Date3 timeintervalsincedate:date2];

NSLog (@ "%f", ti);//2015-04-21 10:10:40.473 oclesson7_nsdate[864:31582] 5.003207

Explain:

Date2 gets the current local time, and then the program continues execution, when it gets date3, the time has elapsed for 5 seconds (because sleep5 seconds), at which time Date3 is 5 seconds after date2 time. The time difference between date3 and Date2 is calculated using the Timeintervalsincedate method, which is 5 seconds.

Nstimeinterval is another name of type double

This nstimeinterval is produced by a typedef double nstimeinterval; The defined variables are also of type double.

Datebyaddingtimeinterval for a time offset

The Datebyaddingtimeinterval method is to increase or decrease a time difference (the difference) for a period, which can compensate for the lack of sleep well. Because the parameter of this method is a double data, what is the value passed in, the time will be added or subtracted from this value, to get a new time.

NSDate *date4 = [[NSDate alloc]initwithtimeintervalsincenow:offset];

NSLog (@ "%@", Date4)//2015-04-21 10:15:49.651 oclesson7_nsdate[896:33153] 2015-04-21 10:15:49 +0000

NSDate *date5 = [Date4 datebyaddingtimeinterval:60];

NSLog (@ "%@", Date5)//2015-04-21 10:15:49.651 oclesson7_nsdate[896:33153] 2015-04-21 10:16:49 +0000

In the example, Date5 is 60 seconds faster than Date4.

An example:

Practice, the current time with a fixed time difference, in 60 seconds, output just, 60~3600, output a few minutes ago, >3600 seconds, output a few hours ago

Practice, the current time with a fixed time difference, in 60 seconds, output just, 60~3600, output a few minutes ago, >3600 seconds, output a few hours ago

Nstimezone *zone1 = [Nstimezone localtimezone];

Nsinteger Offset1 = [Zone1 secondsfromgmt];

NSDate *time1 = [[NSDate Alloc]initwithtimeintervalsincenow:offset1];

NSDate *DATE21 = [time1 datebyaddingtimeinterval:-6*4];

NSDate *date31 = [[NSDate Alloc]initwithtimeintervalsincenow:offset1];

Nstimeinterval ti1 = [Date31 timeintervalsincedate:date21];

if (Ti1 < 60) {

NSLog (@ "just");

}else if (ti1 >=60 && ti1 < 3600) {

NSLog (@ "%.f minutes ago:", (TI1/60));

}else{

NSLog (@ "%.f hours ago:", (TI1/60/60));

}

NSDate (16)

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.