IOS -Date
2015-09-02 16:45
429 查看
去除昨天上周,上月,上一年等
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);
相关文章推荐
- iOS之集合对象:将NSArray和NSDictionary两个集合对象写到一个文件中
- ios开发时,键盘遮挡输入框解决方法
- runtime 运行时机制 完全解读
- iOS开发日记14-NSFileManager(沙盒和清除缓存)
- iOS 内存管理 (有ARC)
- iOS中Cell高度如何实现自动适应内容
- IOS @2X.png
- iOS9新特性 - Search APIs
- 关于iOS地图定位中点击设置->隐私->定位服务 闪退问题
- iOS学习——AES加密
- iOS开发:AVPlayer实现流音频边播边存
- ios横竖屏配置subViews
- 【iOS开发系列】NSClassFromString、NSSelectorFromString、isKindOfClass、isMemberOfClass
- ios集成支付宝
- IOS的文件I/O
- iOS应用间共享数据
- 国内iOS界最大的行动已经开始了。全面开始翻译iOS开发者文档。
- IOS开发中NSRunloop跟NSTimer的问题
- IOS (null )和<null>的处理
- IOS 页面跳转+Delegate传值