#pragma mark - 获取当前时间戳 -(NSString *)getTimeSp{ NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0]; //返回13位时间戳------12位的去掉 *1000 NSTimeInterval a=[dat timeIntervalSince1970]*1000; NSString *timeString = [NSString stringWithFormat:@"%f", a];//转为字符型 return timeString; } #pragma mark - 获取当前 yyyy-MM-dd HH:mm:ss 格式的时间 -(NSString *)getTime{ NSDate *fromdate=[NSDate date]; NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init]; [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString* string=[dateFormat stringFromDate:fromdate]; return string; } #pragma mark - 将yyyy-MM-dd HH:mm:ss 格式的时间转换成时间戳 /** * timeStr : yyyy-MM-dd HH:mm:ss 格式的时间 */ -(long)changeTimeToTimeSp:(NSString *)timeStr{ long time; NSDateFormatter *format=[[NSDateFormatter alloc] init]; [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *fromdate=[format dateFromString:timeStr]; time= (long)[fromdate timeIntervalSince1970]; return time; } #pragma mark - 将时间戳转为 yyyy-MM-dd HH:mm:ss 格式的时间 /** * timeStr : 10/13位数时间戳 */ -(NSString *)changeTimeSpToTime:(NSString *)timeStr{ unsigned long long createTime ;
/**
* 注意: 如果 timeStr 不是NSString类型,则需要将其转化为 NSString 类型,否则if判断会出错
* 转化代码 :NSString *t =[NSString stringWithFormat:@"%@",timeStr];
*/
if(timeStr.length == 10){ // 10位时间戳 createTime = [timeStr longLongValue]; }else{ // 13位时间戳 createTime = [timeStr longLongValue] / 1000.0; } NSDate *creatDate = [[NSDate alloc] initWithTimeIntervalSince1970:createTime]; NSDateFormatter *creatDateFormatter = [[NSDateFormatter alloc] init]; creatDateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSString *orderTimeEnd = [creatDateFormatter stringFromDate:creatDate]; return orderTimeEnd; }