ios 計算時間差

來源:互聯網
上載者:User

標籤:io   ar   os   sp   for   on   div   2014   log   

//計算上報時間差        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];        [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];//設定你想要的格式,hh與HH的區別:分別表示12小時制,24小時制        NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];        [formatter setTimeZone:timeZone];        NSDate *datenow = [NSDate date];        //設定一個字串的時間        NSMutableString *datestring = [NSMutableString stringWithFormat:@"%@",20141202052740];        //注意 如果20141202052740必須是數字,如果是UNIX時間,不需要下面的插入字串。        [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];        //指定輸出的格式   這裡格式必須是和上面定義字串的格式相同,否則輸出空        [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:@"%@分鐘前", timeString];        }        if (dd/3600>1&&dd/86400<1)        {            timeString = [NSString stringWithFormat:@"%ld", dd/3600];            timeString=[NSString stringWithFormat:@"%@小時前", timeString];        }        if (dd/86400>1)        {            timeString = [NSString stringWithFormat:@"%ld", dd/86400];            timeString=[NSString stringWithFormat:@"%@天前", timeString];        }        NSLog(@"=====%@",timeString);

ios 計算時間差

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.