Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coderChrisLee/6c144039378a57935f347cd5aff73bc6 to your computer and use it in GitHub Desktop.
Save coderChrisLee/6c144039378a57935f347cd5aff73bc6 to your computer and use it in GitHub Desktop.
13位时间戳(毫秒)与10位时间戳处理——服务器一般是13位时间戳。
//获取当前时间
- (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;
}
//字符串转时间戳 如:2017-4-10 17:15:10
- (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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment