博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 时间和时间戳之间转化
阅读量:4630 次
发布时间:2019-06-09

本文共 1575 字,大约阅读时间需要 5 分钟。

以毫秒为整数值的时间戳转换

  • 时间戳转化为时间NSDate
- (NSString *)timeWithTimeIntervalString:(NSString *)timeString{    // 格式化时间    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];    [formatter setDateStyle:NSDateFormatterMediumStyle];    [formatter setTimeStyle:NSDateFormatterShortStyle];    [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];        // 毫秒值转化为秒    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];    NSString* dateString = [formatter stringFromDate:date];    return dateString;}
  • 时间转化为时间戳
// 当前时间     NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];    NSTimeInterval a=[date timeIntervalSince1970]*1000; // *1000 是精确到毫秒,不乘就是精确到秒    NSString *timeString = [NSString stringWithFormat:@"%.0f", a]; //转为字符型
  • 通过比较时间与当前时间返回年月日的方法
- (void)getBabyDetailAge:(NSString *)date{    // 获得日期对象    NSDateFormatter *formatter_ = [[NSDateFormatter alloc] init];    formatter_.dateFormat = @"yyyy-MM-dd HH:mm:ss";    NSDate *createDate = [formatter_ dateFromString:date];        NSCalendar *gregorian = [[ NSCalendar alloc ] initWithCalendarIdentifier : NSCalendarIdentifierGregorian];    NSUInteger unitFlags = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;    NSDateComponents *components = [gregorian components:unitFlags fromDate:createDate toDate:[NSDate date] options: 0 ];        NSInteger years = [components year];    NSInteger months = [components month ];    NSInteger days = [components day ];}

转载于:https://www.cnblogs.com/ShaoYinling/p/5022404.html

你可能感兴趣的文章
设计模式之代理模式
查看>>
看盘经验一
查看>>
2015.11.23(利亚德在11点涨停,结果大笔进货,结果又下来了——被套)
查看>>
Linux下的SVN服务器搭建
查看>>
工作中几个点回想
查看>>
配合crond服务实现自定义周期备份MySQL数据库(使用innobackupex进行备份)
查看>>
Mysql数据库调优和性能优化
查看>>
黑马程序猿_try-catch-finally
查看>>
epoll使用具体解释(精髓)
查看>>
SkinSharp用法
查看>>
What is JWT?
查看>>
移动端头
查看>>
服务器操作系统应该选择 Debian/Ubuntu 还是 CentOS?
查看>>
前端解决跨域问题
查看>>
【例2-3】围圈报数
查看>>
第一章:进程与线程总结
查看>>
1942
查看>>
2014-07-29 18:12
查看>>
初学SpringBoot之二
查看>>
EasyUi中使用自定义图标
查看>>