< 返回首页

标哥的笔记,是记录在日常学习技术和日常开发中那些年遇到过的坑!本站为新站,原"标哥的技术博客"中的文章会慢慢移到本站,欢迎收藏本站!
在使用本站过程中,有任何建议请联系标哥! 另,承接App开发、网站开发和微信小程序开发!欢迎联系我们


iOS日期与时间戳互转

 作者:标哥    发布日期:2017-01-11 16:26    阅读量:1089次
 

在开发中经常操作日期与时间戳,这只是日期与时间戳互转的代码片段!

下面这两个方法是给NSDate的扩展方法来实现的,大家只需要将代码复制过去就可以了!

将对象转换成时间戳

事实上,由日期类转换成时间戳是很容易的,是相对于1970年的:

- (NSString *)hyb_toTimeStamp {
  return [NSString stringWithFormat:@"%lf", [self timeIntervalSince1970]];
}

将时间戳转换成对象

将时间戳转换成日期对象也很容易,也要基于1970年计算:

+ (NSDate *)hyb_toDateWithTimeStamp:(NSString *)timeStamp {
  NSString *arg = timeStamp;
  
  if (![timeStamp isKindOfClass:[NSString class]]) {
    arg = [NSString stringWithFormat:@"%@", timeStamp];
  }
  
  NSTimeInterval time = [timeStamp doubleValue];
  return [NSDate dateWithTimeIntervalSince1970:time];
}

如何使用

日期对象转换成时间戳:

NSDate *date = [NSDate date];
NSString timeStamp = [date hyb_toTimeStamp];

时间戳转换成日期:

NSDate *date = [NSDate hyb_toDateWithTimeStamp:timeStamp];

注意事项

可能有人使用这么转换(网上有人发帖问4s上出现问题而其它手机没有问题):

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];

NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self.appointmentServiceTime integerValue]/1000];

[formatter setDateFormat:@"YYYY.MM.dd"];
NSString *str = [formatter stringFromDate:confromTimesp];

其实问题出现在[self.appointmentServiceTime integerValue],换成doubleValue就都一样了。因为4s上integerValue是int_32,而是5s上integerValue是int_64,所以时间戳这个值是大于32位int值的范围的,所以4s上有问题。

注意:如果后台给你返回来的是时间戳,请不要使用integerValue转换,一定要使用doubleValue。


承接:ThinkPHP项目开发、网站项目开发、微信项目开发、微信小程序项目开发、App开发,欢迎联系标哥QQ632840804