IOS--時間 NSDate,NSTimeInterval的一些轉換

來源:互聯網
上載者:User

標籤:轉換   test   nsstring   date   div   att   amp   sage   class   

 1 //1.時間格式 2 NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init]; 3 [_dateFormatter setDateFormat:@"YYYY年MM月dd天hh小時mm分ss秒"]; 4  5 //2.擷取系統時間 6 NSString *date = [_dateFormatter stringFromDate:[NSDate date]]; 7  8 //3.NSDate轉化為時間戳記 9 NSTimeInterval a=[date timeIntervalSince1970];10 11 //4.時間戳記轉化為NSDate12 NSDate *stampDate = [NSDate dateWithTimeIntervalSince1970:timeInter];13 14 //4.NSDate轉化為NSString15 NSString* dateString = [_dateFormatter stringFromDate:date];16 17 //5.傳入時間差,重設格式18 //傳入時間差,轉換格式19 - (NSString *)timeWithMessageString:(NSTimeInterval)timeInter20 {21     22     int month = timeInter / (3600 * 24 * 30);23     int day = timeInter / (3600 * 24);24     int hour = timeInter / 3600;25     int minute = timeInter / 60;26     27     int day_process = day - month * 30;28     int hour_process = hour - day *24;29     int minute_process = minute - hour *60;30     int miao_process = timeInter - minute*60;31     32     NSString *timedate = nil;33     if (day == 0) {34         timedate = [NSString stringWithFormat:@"%d小時%d分%d秒",hour_process,minute_process,miao_process];35         if (hour == 0) {36             timedate = [NSString stringWithFormat:@"%d分%d秒",minute_process,miao_process];37             if (hour == 0) {38                 timedate = [NSString stringWithFormat:@"%d秒",miao_process];39             }40         }41     }else{42         timedate = [NSString stringWithFormat:@"%d天%d小時%d分%d秒",day_process,hour_process,minute_process,miao_process];43     }44     45     46     timedate = [NSString stringWithFormat:@"限時優惠: 還剩%@",timedate];47     return timedate;48 }

 

IOS--時間 NSDate,NSTimeInterval的一些轉換

相關文章

聯繫我們

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