時間都去哪了?

來源:互聯網
上載者:User

標籤:

       最近一個項目涉及到時間的問題,尤其是目前時間和獲得的時間戳記的轉化,發送當前的時間戳記給伺服器。介面展示的時候呢,展示的是分鐘和秒。這樣就各種計算求解天、小時、分鐘、秒了,雖說簡單,但是真正的寫一下還是要方便許多,所以我封裝了一個小方法,分享下:【個人使用,如有更最佳化的方法,還望不吝賜教】話不多說上代碼:

-(NSString * )timeDifference:(NSString *)timeStamp {        //     將時間戳記轉換成字串        NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[timeStamp intValue]];        NSTimeZone * zone = [NSTimeZone systemTimeZone];        NSInteger interval =[zone secondsFromGMTForDate:confromTimesp];        NSDate *date1 = [confromTimesp dateByAddingTimeInterval:interval];                    NSDate * dateNow = [self getCurrentTime];            NSLog(@"時間戳記對應的%@",date1);            NSLog(@"目前時間對應的%@",dateNow);            NSTimeInterval time=[dateNow timeIntervalSinceDate:date1];        int days=((int)time)/(3600*24);    int hours=((int)time)%(3600*24)/3600;    int minute=((int)time)%(3600*24)/60-hours*60;    int second = (int)time - minute*60 - hours*3600 -days*24*3600;    NSString *dateContent=[[NSString alloc] initWithFormat:@"%i天%i小時%i分鐘%i秒",days,hours,minute,second];        //    NSString * differenceTime = [NSString stringWithFormat:@"%i分%i秒",minute,second];        NSLog(@"日期之間的時間差:%@",dateContent);    return dateContent;    }

這個方法是擷取一個時間戳記,將這個時間戳記和目前時間作差,獲得時間間隔,傳回值就比較隨意了,我是把所有的都返回了,根據需要進行刪減吧。發送目前時間戳也很簡單,直接一句代碼:

      NSDate * sendate = [NSDate date];          NSString * date2  = [NSString stringWithFormat:@"%ld",(long)[sendate  timeIntervalSince1970]];          NSLog(@"當前的時間戳記為:%@",date2);    

 

時間都去哪了?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.