Ios_ Timestamp and date conversion (a few minutes ago)

Source: Internet
Author: User

Mark for myself.

When the time stamp turns, it often encounters the type to be converted to a few minutes ago. For example, the latest product theory was published "one minute Ago" or "10 minutes ago".

The following method encapsulates the time stamp that is encountered during the development process for the type string and other timestamp and date conversions:

#define MARK-Time/** * Timestamp converted to String * * @param timestamp timestamp * * @return formatted string */+ (NSString *) Timefromtimestamp: (Nsinte    GER) timestamp{NSDateFormatter *dateformtter =[[nsdateformatter alloc] init];    NSDate *d = [NSDate datewithtimeintervalsince1970:timestamp];    Nstimeinterval late=[d timeintervalsince1970]*1;    Time stamp nsdate* dat = [nsdate datewithtimeintervalsincenow:0];   Nstimeinterval Now=[dat timeintervalsince1970]*1;    Get current timestamp nsstring *[email protected] "";    Nstimeinterval cha=now-late;        Published within one 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]; }//within one hour or more 24 or less else if (cha/3600>1&&cha/86400<1) {timestring = [NSString STRINGWITHFormat:@ "%f", cha/3600];        TimeString = [TimeString substringtoindex:timestring.length-7];    Timestring=[nsstring stringwithformat:@ "%@ hours Ago", timestring];        }//Posted in more than 24 10 days if else if (cha/86400>1&&cha/86400*3<1)//86400 = 60 (min) *60 (sec) *24 (hours) within 3 days {        TimeString = [NSString stringwithformat:@ "%f", cha/86400];        TimeString = [TimeString substringtoindex:timestring.length-7];    Timestring=[nsstring stringwithformat:@ "%@ days Ago", timestring];        }//Publication time greater than 10 days else {[Dateformtter setdateformat:@ "Yyyy-mm-dd"];    TimeString = [Dateformtter stringfromdate:d]; } return timestring;} /** * Convert timestamp to TIME according to format * * @param timestamp timestamp * @param dateformtter date format * * @return formatted date */+ (NSString *) Timefrom Timestamp: (Nsinteger) Timestamp formtter: (NSString *) formtter{nsdateformatter *dataformtter =[[nsdateformatter alloc    ] init];    [Dataformtter Setdateformat:formtter]; NSDate *date = [NSDate datewithtimeintervalsincE1970:timestamp];    NSString *time = [Dataformtter stringfromdate:date]; return time;}    /** * Get current timestamp */+ (NSString *) timeintervalgetfromnow{//Get Time (non-local time zone, need to convert) nsdate * today = [NSDate Date];    Nstimezone *zone = [Nstimezone systemtimezone];    Nsinteger interval = [zone secondsfromgmtfordate:today];    Convert to local time nsdate *localedate = [Today Datebyaddingtimeinterval:interval];        Time converted to timestamp nsstring *TIMESP = [NSString stringwithformat:@ "%ld", (long) [Localedate timeIntervalSince1970]]; return TIMESP;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Ios_ Timestamp and date conversion (a few minutes ago)

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.