当我们开发中经常遇到时间戳转换成时间,我们遇到的时间戳分两种类型.

1.2233445673

2.3456677890498

这两种其实没什么区别,13为的后三位是精度值

我们计算的时候可以去掉

1,计算当前的时间

NSDate *nowDate = [NSDate date];

获取到当前时间:

nowDate=====2015-07-18 03:16:50 +0000

2,把当前时间转成"yyyy-MM-dd"

NSDateFormatter *etimeFormate = [[NSDateFormatter alloc]init];

[etimeFormate setDateFormat:@"yyyy-MM-dd"];

NSString *etimeStr = [etimeFormate stringFromDate:[NSDate date]];

3.时间戳转换成日期

stime = @"1234567890123" ;

NSTimeInterval time = ([strTime doubleValue]/1000);

NSDate *date= [NSDate dateWithTimeIntervalSince1970:time];

NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

[formatter setDateFormat:@"yyyy-MM-dd"];

NSString *CurrDate = [formatter stringFromDate:date];

把同一时间戳转换成时间

NSDateFormatter *twoFormat = [[NSDateFormatter alloc]init];

[twoFormat setDateFormat:@"HH:mm:ss"];

NSString *timeDate = [twoFormat stringFromDate:date];

4.计算几天前的日期

//更改当前时间格式

NSInteger dis = 30;//多少天前

NSTimeInterval oneDay = 24*60*60*1;//一天的时间

NSDate *nowDate = [NSDate date];

NSDate *stimeDate = [nowDate initWithTimeIntervalSinceNow:-oneDay * dis];//用当前时间转换

NSDateFormatter *stimeFormatter = [[NSDateFormatter alloc]init];//转换

[stimeFormatter setDateFormat:@"yyyy-MM-dd"];

NSString *stimeStr = [stimeFormatter stringFromDate:stimeDate]

总结:

我们经常用到的就是NSDateFormatter stringFromDate

NSDate有几种方法

- (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;//从现在算

- (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs;//1970年开始

- (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;

星期的转换方式和天的转换

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *now;

NSDateComponents *comps = [[NSDateComponents alloc] init];

NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |

NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

now=[NSDate date];

comps = [calendar components:unitFlags fromDate:now];

NSInteger   week = [comps weekday];//特殊说明:week = 1 周日 =2 周一 =3周二 ...  7周六

//下面的大家打印一下就能看明白,程序员要动手,不能光看

NSInteger   month = [comps month];

NSInteger   day = [comps day];

NSInteger   hour = [comps hour];

NSInteger  min = [comps minute];

NSInteger    sec = [comps second];

转载于:https://www.cnblogs.com/runningsoul/p/4656452.html

时间戳的转换和星期转换相关推荐

  1. php date hi,php中时间戳和日期格式的转换

    原文:php中时间戳和日期格式的转换 一,PHP时间戳函数获取指定日期的unix时间戳 strtotime("2009-1-22″) 示例如下: echo strtotime("2 ...

  2. mysql13位的时间戳怎么转化_MySQL时间函数 | 时间戳和日期之间得转换

    首页 专栏 mysql 文章详情 0 MySQL时间函数 | 时间戳和日期之间得转换 阿壮Jonsson 发布于 2 月 6 日 一.时间戳转日期 select FROM_UNIXTIME(16060 ...

  3. 时间戳与时间之间的转换

    时间戳:是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数. 1.Java中转换: 时间戳转换为时间: Long timeSt ...

  4. 将时间戳“年月日 时分秒”格式转换成“年月日”格式

    将时间戳"年月日 时分秒"格式转换成"年月日"格式 将时间戳"年月日 时分秒"格式转换成"年月日"格式 总是忘记要重新查 ...

  5. 时间戳转换,在线转换时间戳

    时间戳转换,在线转换时间戳 自己学习新框架时写的一个工具站,欢迎使用 点击跳转

  6. 前端开发:JS中时间戳和时间之间的转换

    前言 在前端开发过程中,关于时间相关的业务处理是非常常见的需求,需求也是千奇百怪,各种各样的需求,在JS中对于时间戳和时间之间的转换也是非常简单的,难就难在转换后的时间格式和业务需求的格式要匹配一致, ...

  7. Java日期格式转换成星期几

    日期格式YYYY-MM-DD: //Java日期格式转换成星期几 yyyy-MM-ddpublic static String dateToWeek(String datetime) throws P ...

  8. 首师大附中OJ系统 0033 数字和星期转换

    数字和星期转换 难度级别:A: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 输入一个正整数,如果是 1 就输出"星期一" ...

  9. pdf转ppt怎么转换,pdf转换ppt的方法分享

    工作中为了方便我们在对文件的查看和应用,都会遇到过文件格式的相互转换吧,如果在工作中碰到了PDF文件转换成PPT文件的时候,我们要用什么样的方式转换最容易而又不出错呢,下面小编为大家分享一下小编常用的 ...

最新文章

  1. mysql 开启慢查询命令【转】
  2. Android开发之大位图二次採样压缩处理(源码分享)
  3. Win8.1无法安装更新,提示0x800*****错误的解决方法
  4. 使用PHP CURL的POST数据
  5. Kotlin学习笔记-----函数的定义
  6. 长脖子鹿省选模拟赛 [LnOI2019SP]快速多项式变换(FPT)
  7. GNU make manual 翻译( 一百零四)
  8. CCDP-思科认证网络设计高级工程师
  9. SQLServer LIKE 通配符
  10. 干,认识Audio框架还因此发现一个雷
  11. HDU 5515 Game of Flying Circus 二分
  12. ANDROID 获得地理位置
  13. 90 条 Python 程序建议
  14. oracle gi 创建,浅谈Oracle RAC --GI的启动
  15. 深度学习之神经网络(二)
  16. 中级系统集成项目管理工程师考试记录之总述
  17. Linux+Ubuntu
  18. 10 06 01 繁杂
  19. arm学习方法(转)
  20. 请问苹果x是如何建文件夹_苹果xmind怎么建立文件夹

热门文章

  1. error BK1506
  2. SQL SERVER2005加密解密数据
  3. XMLHttpRequest Object
  4. Ubuntu Server 16.04服务器版配置图解教程06 - 安装MySql
  5. GOCN每日新闻(2017-08-12)
  6. 虚拟机非法关机不能重启了
  7. JavaScript 对象属性作实参以及实参对象的callee属性
  8. Does the “LINQ to Objects” provider have built-in performance optimization?
  9. 使用Javascript制作连续滚动字幕
  10. 随机生成文件名字或随机生成一个数