http://blog.csdn.net/xdrt81y/article/details/8425727

今天跟大家讨论日期的用法,相信大家在项目中,经常会设置一个默认时间段,比如一周前到今天。下面教大家怎么获取前一天,或前一周等等。

比如date 2009-12-11

NSDate *today = [NSDate dateWithString:@"2009-12-11 00:00:00 +0000"];
NSDate *yesterday = [NSDate dateWithString:@"2009-12-10 00:00:00 +0000"];
NSDate *thisWeek = [NSDate dateWithString:@"2009-12-06 00:00:00 +0000"];
NSDate *lastWeek = [NSDate dateWithString:@"2009-11-30 00:00:00 +0000"];
NSDate *thisMonth = [NSDate dateWithString:@"2009-12-01 00:00:00 +0000"];
NSDate *lastMonth = [NSDate dateWithString:@"2009-11-01 00:00:00 +0000"];

要求出上面的时间

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]]; [components setHour:-[components hour]];
[components setMinute:-[components minute]];
[components setSecond:-[components second]];
NSDate *today = [cal dateByAddingComponents:components toDate:[[NSDate alloc] init] options:0]; //This variable should now be pointing at a date object that is the start of today (midnight); [components setHour:-24];
[components setMinute:0];
[components setSecond:0];
NSDate *yesterday = [cal dateByAddingComponents:components toDate: today options:0]; components = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[[NSDate alloc] init]]; [components setDay:([components day] - ([components weekday] - 1))];
NSDate *thisWeek = [cal dateFromComponents:components]; [components setDay:([components day] - 7)];
NSDate *lastWeek = [cal dateFromComponents:components]; [components setDay:([components day] - ([components day] -1))];
NSDate *thisMonth = [cal dateFromComponents:components]; [components setMonth:([components month] - 1)];
NSDate *lastMonth = [cal dateFromComponents:components]; NSLog(@"today=%@",today);
NSLog(@"yesterday=%@",yesterday);
NSLog(@"thisWeek=%@",thisWeek);
NSLog(@"lastWeek=%@",lastWeek);
NSLog(@"thisMonth=%@",thisMonth);
NSLog(@"lastMonth=%@",lastMonth);

正在学习过程中,错误之处请指正,欢迎交流,共同学习;

欢迎转载分享,请注明出处http://blog.csdn.net/xdrt81y

IOS-NSDate之今天,昨天,这周,这个月,上个月的更多相关文章

  1. IOS NSDate NSDateFormatter 导致相差8小时

    时间问题应该是所有编程语言都要处理的.详细学过php的同学知道,php中也会有相差8小时的问题,然而php可以非常方便的解决的,直接设置下就好了 我最近在学习IOS的过程中,发现IOS的日期处理也是个 ...

  2. mysql查询昨天 一周前 一月前 一年前的数据

    mysql 昨天 一周前 一月前 一年前的数据 这里主要用到了DATE_SUB, 参考如下 代码如下: SELECT * FROM yh_contentwhere inputtime>DATE_ ...

  3. C#根据当前时间获取周,月,季度,年度等时间段的起止时间

    最近有个统计分布的需求,需要按统计本周,上周,本月,上月,本季度,上季度,本年度,上年度等时间统计分布趋势,所以这里就涉及到计算周,月,季度,年度等的起止时间了,下面总结一下C#中关于根据当前时间获取 ...

  4. MySQL中获取天、周、月等数据

    MySQL中获取天.周.月等数据 1.今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 2.昨天 SELECT * FROM 表名 ...

  5. Mysql 查询天、周,月,季度、年的数据

    Mysql 查询天.周,月,季度.年的数据 今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 ...

  6. 用sql查询当天,一周,一个月的数据

    用sql查询当天,一周,一个月的数据   数据查询,不管在网站还是在系统,都很常见,下文是介绍最常见的以日期查询的语句 select * from ShopOrder where datediff(w ...

  7. 用js获取周、月第一天和最后一天(转载)

    var getCurrentWeek = function (day) { var days = ["周日", "周一", "周二", &q ...

  8. DB查询分析器7.01新增的周、月SQL执行计划功能

                DB查询分析器7.01新增的周.月SQL执行计划功能 马根峰              (广东联合电子服务股份有限公司, 广州 510300) 1      引言   中国本土 ...

  9. PHP 获取周,月列表

    PHP的date函数以及strtotime函数是很强大的.基本上围绕这2个函数就能处理绝大多数日常开发中日期的处理. 假设有一个需求是按周,月获取最近7周和最近7月的查询.那么我们肯定要划分出时间区间 ...

  10. [转]Java中一周前一个月前时间计算方法

    Java中一周前一个月前时间计算方法 在java语言中,用如下方法获取系统时间: Date date = new Date(); String year=new SimpleDateFormat(&q ...

随机推荐

  1. POJ:1064-Cable master

    Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 58613 Accepted: 12231 Descri ...

  2. Y86模拟器的安装

    说白了就几个指令,跟实验楼里面并不完全一样. tar -xvf sim.tar cd sim sudo apt-get install tk sudo ln -s /usr/lib/x86_64-li ...

  3. 9,Linux下的python3,virtualenv,Mysql、nginx、redis安装配置

    常用服务安装部署   学了前面的Linux基础,想必童鞋们是不是更感兴趣了?接下来就学习常用服务部署吧! 安装环境: centos7 + vmware + xshell MYSQL(mariadb) ...

  4. 7 Vue.js实现loading1

    1 2 3 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter ...

  5. erlang中的原子(atom)内部实现[转]

    转自: http://www.kongqingquan.com/archives/208#more-208 Erlang中的atom由通用hash表实现,虚拟机中atom最终的用数值表示,对应表中的下 ...

  6. CentOS 6.0 VNC远程桌面配置[转]

    原文出处: http://blog.haohtml.com/archives/12281 谢谢作者. 引言:必须明白:vncserver在调用的时候,会根据你的配置来启用server端的监听端口,端口 ...

  7. C# Json 序列化大全--任我行

    public class JsonHelper { /// <summary> /// 将Model转换为Json字符串 /// </summary> /// <type ...

  8. Kotlin中when表达式的使用:超强的switch(KAD 13)

    作者:Antonio Leiva 时间:Feb 23, 2017 原文链接:https://antonioleiva.com/when-expression-kotlin/ 在Java(特别是Java ...

  9. 异步fifo的设计(FPGA)

    本文首先对异步 FIFO 设计的重点难点进行分析 最后给出详细代码 一.FIFO简单讲解 FIFO的本质是RAM, 先进先出 重要参数:fifo深度(简单来说就是需要存多少个数据)           ...

  10. leetcode 201. 数字范围按位与 解题报告

    给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点). 示例 1: 输入: [5,7] 输出: 4 ...