iOS Compute time Difference

Source: Internet
Author: User

Calculate escalation Time Difference         nsdateformatter *formatter = [[ nsdateformatter alloc] init];        [formatter  setdateformat:@ "Yyyy-mm-dd hh:mm:ss"];//set the format you want, the difference between HH and HH: 12-hour, 24-hour          nstimezone* timezone = [nstimezone timezonewithname:@ "Asia/Beijing"];         [formatter setTimeZone:timeZone];         NSDate *datenow = [NSDate date];         //set the time of a string         nsmutablestring *datestring  = [nsmutablestring stringwithformat:@ "%@",20141202052740];         //Note   If 20141202052740 must be a number, if it is UNIX time, the following insertion string is not required.         [datestring insertstring: @ "-"  atindex:4];        [datestring insertstring:@ "-"   atindex:7];        [datestring insertstring:@ " "   atindex:10];        [datestring insertstring:@ ":"  atIndex:13 ];        [datestring insertstring:@ ":"  atIndex:16];         nslog (@ "datestring==%@", datestring);         NSDateFormatter * dm = [[NSDateFormatter alloc]init];         //Specifies the format of the output     here the format must be the same as the format of the above definition string, otherwise the output is null          [dm setdateformat:@ "Yyyy-mm-dd hh:mm:ss"];         NSDate * newdate = [dm dateFromString:datestring];         long dd =  (Long) [datenow timeintervalsince1970] - [newdate  Timeintervalsince1970];        nsstring *[email protected] "" ;        if  (dd/3600<1)          {            timestring = [ nsstring stringwithformat:@ "%ld", dd/60];             timestring=[nsstring stringwithformat:@ "%@ minutes Ago", timestring];         }        if  (dd/3600>1&& DD/86400&LT;1)         {             timestring = [nsstring stringwithformat:@ "%ld", dd/3600];             timestring=[nsstring stringwithformat:@ "%@ hours Ago", timeString];         }        if  (dd/86400 &GT;1)         {             timestring = [nsstring stringwithformat:@ "%ld", dd/86400];             timestring=[nsstring stringwithformat:@ "%@ days Ago" , timestring];        }         nslog (@ "=====%@", timestring);

iOS calculation time difference

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.