IOS distance now a few hours, minutes, days, weeks, months, years

Source: Internet
Author: User
Tags dateformat

    nsstring *distancetime = [self returnfromthetimeoftoday:@ " 2016-09-29 01:45:10 "]; #pragma  mark  default one minute has 60 seconds, 60 minutes an hour   24 hours a day, 7 days a week, one months 30 days, 12 months a year   Do not consider other such as common year February 28 days, leap month 29 days these conditions-  (nsstring *) Returnfromthetimeoftoday: (nsstring *) timestr{     //timeStr  string Format->2016-09-29 01:45:10    nsdateformatter * form = [[nsdateformatter alloc] init];    //Setting the time zone      form.locale = [nslocale localewithlocaleidentifier:@ "cn"];     form.dateformat = @ "Yyyy-mm-dd hh:mm:ss";    nsdate *date =  [form datefromstring:timestr];        //Get the current time difference      NSTimeInterval timeInterval = [date timeIntervalSinceNow];     timeinterval = -timeinterval;    //then the comparison of the time     if (timeinterval < 60)      {        return [nsstring stringwithformat:@ "just" ];    }    //min     nsinteger minute =  timeinterval / 60;    if (minute < 60)      {        return [nsstring stringwithformat:@ "%ld minutes ago", minute ];    }    nsinteger hours = minute / 60;     if (hours < 24)     {         return [nsstring stringwithformat:@ "%ld hours ago", hours];    }     nsinteger day = hours / 24;    nsinteger  month = day/30;    nsinteger year = month/12;    if (day <= &NBSP;1)     {        form.dateformat = @ "Hh:mm";        nsstring *oldtime = [form  stringfromdate:date];        return [nsstring  stringwithformat:@ "Yesterday  %@", Oldtime];    }    if (day < &NBSP;7)     {        return [NSString  stringwithformat:@ "%ld days Ago", Day];    }    if ((DAY/7) <4)      {        return [nsstring stringwithformat:@ "% LD weeks Ago ", Day/7];    }    else if (month < 12)      {        return [nsstring stringwithformat:@ "%ld months ago", month];    }     else    {         form.dateformat = @ "yyyy-mm-dd hh:mm";         return  [nsstring stringwithformat:@ "%ld years ago", Year];    }    return  nil;    }


This article is from the "snowflakes flying over the World" blog, please be sure to keep this source http://smengxiang.blog.51cto.com/11204872/1865680

IOS distance now a few hours, minutes, days, weeks, months, years

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.