IOS timestamp conversion to a few just, etc.

Source: Internet
Author: User

The constructor converts the timestamp to a few just/hours ago/a few days ago/a few years ago

Createtimestring 13-bit pure digital time stamp for backstage.

-(NSString *) Updatetimeforrow: (NSString *) createtimestring {//Get current time stamp 1466386762.345715 10-bit integer 6 decimal
Nstimeinterval currenttime = [[NSDate date] timeIntervalSince1970];
Create a song timestamp (the time returned in the background is typically 13 digits)
Nstimeinterval createtime = [createtimestring longlongvalue]/1000;
Time
Nstimeinterval time = Currenttime-createtime;

Nsinteger sec = time/60;
if (sec<60) {
return [NSString stringwithformat:@ "%ld minutes Ago", sec];
}

Second Turn hour
Nsinteger hours = time/3600;
if (hours<24) {
return [NSString stringwithformat:@ "%ld hours ago", hours];
}
Seconds to turn days
Nsinteger days = time/3600/24;
if (days < 30) {
return [NSString stringwithformat:@ "%ld day Ago", days];
}
Second Turn month
Nsinteger months = TIME/3600/24/30;
if (months < 12) {
return [NSString stringwithformat:@ "%ld months ago", months];
}
Seconds following year
Nsinteger years = TIME/3600/24/30/12;
return [NSString stringwithformat:@ "%ld years ago", years];

}



Appendix: If the background gives the time format is: Yyyy-mm-dd HH:mm:ss. Sss

+ (NSString *) Comparecurrenttime: (NSString *) str {//convert string to nsdate nsdateformatter *dateformatter = [[Nsdateforma
    Tter alloc] init]; [Dateformatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss.
    SSS "];

    NSDate *timedate = [Dateformatter datefromstring:str];
    Get with the current time difference nstimeinterval timeinterval = [Timedate Timeintervalsincenow];
    TimeInterval =-timeinterval;
    Standard Time and Beijing 8-hour lag timeinterval = timeinterval-8*60*60;
    Long temp = 0;
    NSString *result;
    if (TimeInterval <) {result = [NSString stringwithformat:@ "just"];
    else if (temp = timeinterval/60) <60 {result = [NSString stringwithformat:@ "%d minutes ago", temp];
    else if (temp = temp/60) <24 {result = [NSString stringwithformat:@ "%d hours ago", temp];
    else if ((temp = temp/24) <30) {result = [NSString stringwithformat:@ '%d days ago ', temp];
    else if (temp = temp/30) <12 {result = [NSString stringwithformat:@ '%d months ago ', temp];
}    else{temp = TEMP/12;
    result = [NSString stringwithformat:@ "%d years ago", temp];
return result; }




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.