Open source China iOS client Learning (ix) time stamp of code fragment

Source: Internet
Author: User

Open source China Client Whether it is a comprehensive article, question or answer questions or move away to say, each behind will be commented on the time, such as "10 minutes Ago" "2 hours before" "5 days Ago" or directly is the time to show "2013-2-9" and so on, these times in many applications are necessary, In order to give users a sense of time, know that the information is the latest, but also convenient to check the past information based on time;

These time from the API parsing time format is YYYY-MM-DD HH:mm:ss (for example: 2013-03-09 09:51:22), through the API to get the article time after the algorithm conversion, in the client source TOOL.M + (NSString * ) Intervalsincenow: (NSString *) thedate implementation of this method: if the information published 1 hours before the display of minutes, the day of information display hours, 10 days by day, 10 days out of the published time to display:

 + (NSString *) Intervalsincenow: (NSString *) thedate {nsdateformatter *date=[[nsdateformatter] init]  
    ;  
    [Date setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];  
    NSDate *d=[date Datefromstring:thedate];  
    Nstimeinterval late=[d timeintervalsince1970]*1;  
    nsdate* dat = [nsdate datewithtimeintervalsincenow:0];  
    Nstimeinterval Now=[dat timeintervalsince1970]*1;  
    NSString *timestring=@ "";  
Nstimeinterval cha=now-late;  
        Published within an hour if (cha/3600<1) {if (cha/60<1) {timestring = @ "1";  
            else {timestring = [nsstring stringwithformat:@ "%f", CHA/60];  
        TimeString = [TimeString substringtoindex:timestring.length-7];  
    } timestring=[nsstring stringwithformat:@ "%@ minutes Ago", timestring]; //In more than 24 hours or less within else if (cha/3600>1&&cha/86400<1) {timestring = [NSString STRINGWITHF ormat:@ "%f", cha/3600];  
        TimeString = [TimeString substringtoindex:timestring.length-7];  
    Timestring=[nsstring stringwithformat:@ "%@ hours Ago", timestring]; //Published in 24 or more 10 days or less else if (cha/86400>1&&cha/864000<1) {timestring = [NSString St  
        ringwithformat:@ "%f", cha/86400];  
        TimeString = [TimeString substringtoindex:timestring.length-7];  
    Timestring=[nsstring stringwithformat:@ "%@ days Ago", timestring];  //Publish time greater than 10 days else {//timestring = [NSString stringwithformat:@ "%d-%"] nsarray *array  
= [Thedate componentsseparatedbystring:@ "];  
        return [array objectatindex:0];  
    timestring = [array objectatindex:0];  
return timestring; }

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.