時間和時間戳記使用

來源:互聯網
上載者:User

標籤:turn   轉化   自己   object   格式   eval   string   test   轉換   

一、擷取目前時間
//擷取目前時間- (NSString *)currentDateStr{    NSDate *currentDate = [NSDate date];//擷取目前時間,日期    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 建立一個時間格式化對象    [dateFormatter setDateFormat:@"YYYY/MM/dd hh:mm:ss SS "];//設定時間格式,這裡可以設定成自己需要的格式    NSString *dateString = [dateFormatter stringFromDate:currentDate];//將時間轉化成字串    return dateString;}
二、擷取目前時間戳
//擷取目前時間戳- (NSString *)currentTimeStr{    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//擷取目前時間0秒後的時間    NSTimeInterval time=[date timeIntervalSince1970]*1000;// *1000 是精確到毫秒,不乘就是精確到秒    NSString *timeString = [NSString stringWithFormat:@"%.0f", time];    return timeString;}
三、時間戳記轉時間
// 時間戳記轉時間,時間戳記為13位是精確到毫秒的,10位精確到秒- (NSString *)getDateStringWithTimeStr:(NSString *)str{    NSTimeInterval time=[str doubleValue]/1000;//傳入的時間戳記str如果是精確到毫秒的記得要/1000    NSDate *detailDate=[NSDate dateWithTimeIntervalSince1970:time];    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; //執行個體化一個NSDateFormatter對象    //設定時間格式,這裡可以設定成自己需要的格式        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss SS"];    NSString *currentDateStr = [dateFormatter stringFromDate: detailDate];    return currentDateStr;}
四、字串轉時間戳記
//字串轉時間戳記- (NSString *)getTimeStrWithString:(NSString *)str{    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 建立一個時間格式化對象    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; //設定時間的格式    NSDate *tempDate = [dateFormatter dateFromString:str];//將字串轉換為時間對象    NSString *timeStr = [NSString stringWithFormat:@"%ld", (long)[tempDate timeIntervalSince1970]*1000];//字串轉成時間戳記,精確到毫秒*1000    return timeStr;}

時間和時間戳記使用

聯繫我們

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