Get current time, set time, and calculate travel value in iOS

Source: Internet
Author: User
Tags set time

1NSDate *date = [NSDate Date];//Get current Time2Nstimezone *zone = [Nstimezone systemtimezone];//Modify Time Zone3Nsinteger interval1 = [zone secondsfromgmtfordate:date];//Modify Time Zone4_localdate1 = [date Datebyaddingtimeinterval:interval1];//Modify Time Zone5NSLog (@"~~~~~~~~~~~~~ Today%@\n", _localdate1);6         7         8Nscalendar *calendar =[Nscalendar Currentcalendar];9nsdatecomponents *comp = [Calendar Components:nsyearcalendarunit | Nsmonthcalendarunit | Nsdaycalendarunit | Nsweekdaycalendarunit |Nsdaycalendarunit fromdate:_localdate1];Ten  One         //get current Month day ANsinteger year =[comp year]; -Nsinteger month =[Comp month]; -Nsinteger Day =[Comp Day]; the          -nsdatecomponents * Components =[[Nsdatecomponents alloc]init]; -  -         //setting + [Components setyear:year]; - [Components Setmonth:month]; + [Components Setday:day]; A[Components Sethour: -]; at[Components Setminute: +]; -          -Nscalendar *current =[Nscalendar Currentcalendar]; -          -NSDate *setdate1 =[Current datefromcomponents:components]; -         //The default time zone is Greenwich 0 time zone, we are in the East 8 area inNstimezone *zone2 = [Nstimezone systemtimezone];//Modify Time Zone -Nsinteger Interval2 = [Zone2 secondsfromgmtfordate:setdate1];//Modify Time Zone to_localdate2 = [SetDate1 datebyaddingtimeinterval:interval2];//Modify Time Zone +          -NSLog (@"-----------%@\n", _localdate2); the //_localdate1-_localdate2, the result of the default is the second, you can convert *Nsinteger lasttime = [_localdate2 timeintervalsincedate:_localdate1]/ -; $NSLog (@"!!!!!!!!!!!! %ld\n", lasttime);

Day will be added one more days, do not know what is the situation

Get current time, set time, and calculate travel value in iOS

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.