標籤: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;}
時間和時間戳記使用