NSDate和NSDateFormatter 相关应用代码示例
此方法用来计算当前时间与目标时间的先后顺序:
-(NSDate *)calculateTimeWithCurrentTime:(NSDate *)currentDate{
//将当前时间转为本地时区
NSDate * currentLocalDate = [self localDateFromDate:currentDate];
NSLog(@"currentDate--->%@",currentLocalDate);
//设置目标时间为当天15:30
NSString *dateStr = [NSString stringWithFormat:@"%@",currentLocalDate];
NSString *dateString = [dateStr substringToIndex:11];
NSString *targetDateStr = [NSString stringWithFormat:@"%@15:30:00",dateString];
NSDateFormatter *formatter001 = [[NSDateFormatter alloc] init];
[formatter001 setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSDate * targetDate = [formatter001 dateFromString:targetDateStr];
//将目标时间转为本地时区
targetDate = [self localDateFromDate:targetDate];
NSLog(@"targetDate--->%@",targetDate);
//比较当前时间和目标时间
NSComparisonResult result = [currentLocalDate compare:targetDate];
NSLog(@"result--->%d",result);
//获取当前日期是星期几
NSString *weekDay = [self weekdayStringFromDate:currentDate];
NSLog(@"weekDay--->%@",weekDay);
//计算收益时间
NSDate *calDate;
if ([weekDay isEqualToString:@"周一"] || [weekDay isEqualToString:@"周二"] || [weekDay isEqualToString:@"周三"] ) {
//升序
if (result == NSOrderedAscending) {
NSLog(@"15:30之前");
calDate = [currentDate dateByAddingTimeInterval:aDay];
}
else{
NSLog(@"15:30之后");
calDate = [currentDate dateByAddingTimeInterval:2*aDay];
}
}
else if ([weekDay isEqualToString:@"周四"]){
//升序
if (result == NSOrderedAscending) {
calDate = [currentDate dateByAddingTimeInterval:aDay];
}
else{
calDate = [currentDate dateByAddingTimeInterval:4*aDay];
}
}
else if ([weekDay isEqualToString:@"周五"]){
//升序
if (result == NSOrderedAscending) {
calDate = [currentDate dateByAddingTimeInterval:3*aDay];
}
else{
calDate = [currentDate dateByAddingTimeInterval:4*aDay];
}
}
else if ([weekDay isEqualToString:@"周六"]){
calDate = [currentDate dateByAddingTimeInterval:3*aDay];
}
else if ([weekDay isEqualToString:@"周日"]){
calDate = [currentDate dateByAddingTimeInterval:2*aDay];
}
calDate = [self localDateFromDate:calDate];
return calDate;
}
将时区转为当前时区
- (NSDate *)localDateFromDate:(NSDate *)date{
//获取系统时区
NSTimeZone *zone = [NSTimeZone systemTimeZone];
//时间间隔
NSInteger interval = [zone secondsFromGMTForDate: date];
//得到当前时间
NSDate *localeDate = [date dateByAddingTimeInterval: interval];
return localeDate;
}
判断当天是星期几:
- (NSString*)weekdayStringFromDate:(NSDate*)inputDate {
NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null],@"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
[calendar setTimeZone: timeZone];
NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
return [weekdays objectAtIndex:theComponents.weekday];
}
NSDate和NSDateFormatter 相关应用代码示例的更多相关文章
- 推荐Java五大微服务器及其代码示例教程
来源素文宅博客:http://blog.yoodb.com/yoodb/article/detail/1339 微服务越来越多地用于开发领域,因为开发人员致力于创建更大,更复杂的应用程序,这些应用程序 ...
- Java基础知识强化之IO流笔记72:NIO之 NIO核心组件(NIO使用代码示例)
1.Java NIO 由以下几个核心部分组成: Channels(通道) Buffers(缓冲区) Selectors(选择器) 虽然Java NIO 中除此之外还有很多类和组件,Channel,Bu ...
- Hadoop RCFile存储格式详解(源码分析、代码示例)
RCFile RCFile全称Record Columnar File,列式记录文件,是一种类似于SequenceFile的键值对(Key/Value Pairs)数据文件. 关键词:Reco ...
- Delphi之通过代码示例学习XML解析、StringReplace的用法(异常控制 good)
*Delphi之通过代码示例学习XML解析.StringReplace的用法 这个程序可以用于解析任何合法的XML字符串. 首先是看一下程序的运行效果: 以解析这样一个XML的字符串为例: <? ...
- [代码示例]用Fine Uploader+ASP.NET MVC实现ajax文件上传
原文 [代码示例]用Fine Uploader+ASP.NET MVC实现ajax文件上传 Fine Uploader(http://fineuploader.com/)是一个实现 ajax 上传文件 ...
- 分享用于学习C++音频处理的代码示例
与<分享用于学习C++图像处理的代码示例>为姊妹篇. 为了便于学习C++音频处理并研究音频算法, 俺写了一个适合初学者学习的小小框架. 麻雀虽小五脏俱全,仅仅考虑单通道处理. 采用Deco ...
- JAVA NIO工作原理及代码示例
简介:本文主要介绍了JAVA NIO中的Buffer, Channel, Selector的工作原理以及使用它们的若干注意事项,最后是利用它们实现服务器和客户端通信的代码实例. 欢迎探讨,如有错误敬请 ...
- Ice简介+Qt代码示例
1.ICE是什么? ICE是ZEROC的开源通信协议产品,它的全称是:The Internet Communications Engine,翻译为中文是互联网通信引擎,是一个面向对象的中间件,它封装并 ...
- 英特尔® 实感™ 深度摄像头代码示例 – R200 摄像头数据流
英特尔开发人员专区原文地址 简介 该可下载代码示例展示了如何使用面向 Windows 的英特尔® 实感™ SDK* 捕捉和查看用 C#/XAML 编写的原始 R200 摄像头数据流. Visual S ...
随机推荐
- mac os 下的sublime --- 快捷键
mac os 下的sublime ---列模式 http://www.oschina.net/question/249672_161413 触控板也可以的:左手同时按住 Option 和触控板左下角 ...
- LeetCode Power of Four
原题链接在这里:https://leetcode.com/problems/power-of-four/ 题目: Given an integer (signed 32 bits), write a ...
- sql之连表查询--效率 通过分析各种连接查询的实现原理来了解
1. 左连接 2.右连接 3.内连接 4.Cross join 笛卡尔乘积
- ios - runtime运行时应用---交换方法
runtime运行时用法之一 --- 交换类的方法,此处简单写了把系统的UIView的setBackgroundColor的方法换成了自定义的pb_setBackgroundColor 首先创建UIV ...
- js数字位数太大导致参数精度丢失问题
最近遇到个比较奇怪的问题,js函数里传参,传一个位数比较大,打印arguments可以看到传过来的参数已经改变. 然后查了一下,发现确实是js精度丢失造成的.我的解决方法是将数字型改成字符型传输,这样 ...
- 【详解】ERP、APS与MES系统是什么?
ERP是什么?MES是什么?APS又是什么?无论他们有什么功能,对企业有什么意义,不过都是计算机在读写一些数据而已.实际上这一切的本质不过是数据在硬盘和内存中快速的读和写. ERP是--,APS是-- ...
- android switch语句报错:case expressions must be constant expressions
今天无意中碰见了 case expressions must be constant expressions 的问题 写了一个 switch(item.getItemId()) { case R. ...
- mybatis里的foreach语句
相信用了Mybatis的朋友们,都曾有一个疑惑,就是foreach是怎么用的,下面我就简单讲讲我的理解: foreach主要用在SQL语句中迭代一个集合.foreach元素的属性主要由item,ind ...
- Unit01: JAVA开发环境案例
Top JAVA Fundamental DAY01 JDK及Eclipse目录结构操作 JDK的安装及配置 控制台版的JAVA HelloWorld 使用Eclipse开发Java应用程序 1 JD ...
- Windows Phone 二、WP控件