IOS Countdown and Access time in this time zone

Source: Internet
Author: User

Countdown

Write a timer in Viewdidload.

[Nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (timerfiremethod:) Userinfo:nil repeats: YES];

Then the method of declaring the timer

-(void) Timerfiremethod: (nstimer*) Thetimer

{

Define a Nscalendar Object

Nscalendar *cal = [Nscalendar Currentcalendar];

Initialize target time

Nsdatecomponents *shibo = [[Nsdatecomponents alloc] init]; [Shibo setyear:2014];

[Shibo Setmonth:12];

[Shibo setday:21];

[Shibo sethour:16];

[Shibo setminute:30];

[Shibo setsecond:0];

Load the target time into date

NSDate *todate = [cal Datefromcomponents:shibo];

Get current time

NSDate *today = [NSDate Date];

To get a specific time difference.

unsigned int unitflags = Nsyearcalendarunit | Nsmonthcalendarunit | Nsdaycalendarunit | Nshourcalendarunit | Nsminutecalendarunit | Nssecondcalendarunit;

Nsdatecomponents *d = [cal components:unitflags fromdate:today todate:todate options:0];

Lab is a global variable

Lab.text = [NSString stringwithformat:@ "%d%d months%d days%d"%d seconds ", [D year],[d Month], [D day],[d Hour], [D minute], [D second]];

}

Get time in this time zone

The time taken by this method is not local time, but GMT time

NSDate *gmtdate = [NSDate Date];

NSLog (@ "currentdate%@", gmtdate);//currentdate 2014-12-04 05:49:57 +0000

local time

Nstimezone *timezone = [Nstimezone systemtimezone];

Nsinteger interval = [TimeZone secondsfromgmt];

NSDate *localedate = [Gmtdate datebyaddingtimeinterval:interval];

NSLog (@ "Localedate%@", localedate);

IOS Countdown and Access time in this time zone

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.