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 ...
随机推荐
- mui框架使用的过程中遇到的几个问题
1.zepto.js和mui一起使用的时候,tap事件会发生两次,这时只要不引用zepto.js的touch.js就可以了,只用mui的tap事件,如: mui(".infor_header ...
- 一次性搞明白 service和factory区别
原文链接 http://blog.thoughtram.io/angular/2015/07/07/service-vs-factory-once-and-for-all.html 等下,已经有一篇文 ...
- 2003-can't connect to mysql server on 'localhost'(10061) MySQL错误
开始遇到这个问题,我以为是服务没链接,在网上查了下错误,但没解决,后来链接了下端口,结果发现我原来我 端口不对,MySQL端口默认是3306我安装时端口是设的3307.希望能帮到遇到这种问题的人
- ARC 与非 ARC 之间那些的'祸害'
你是否也曾被 assign.retain.copy.release.autorelease.strong.__strong.weak.__weak.__unsafe__unretain.__autor ...
- 移动Web开发调研
背景 在移动互联网浪潮下,移动设备普及,对配置需要考虑移动端设备可访问性.Web作为最贴近用户的配置手段,面向从PC端传统页面,向移动端页面的转型. 概念 PC Web: 面向传统PC电脑的浏览器开发 ...
- sql服务器启动不了问题
问题:the the service mysql56 failed the most recent status change request with the messagethe service ...
- LDAP 中 CN, OU, DC 的含义
1. LDAP的存储规则 区分名(DN,Distinguished Name) 和自然界中的树不同,文件系统/LDAP/电话号码簿目录的每一片枝叶都至少有一个独一无二的属性,这一属性可以帮助我们来区别 ...
- noi 1.5 43:质因数分解
描述 已知正整数 n 是两个不同的质数的乘积,试求出较大的那个质数. 输入 输入只有一行,包含一个正整数 n.对于60%的数据,6 ≤ n ≤ 1000.对于100%的数据,6 ≤ n ≤ 2*10^ ...
- 命令行下Git的使用
命令行下Git的使用 写在前边的话 以自己即将进行的毕设项目为例,进行Git使用的简易说明.不过由于校园网络的限制,故使用GitOSC. 快速开始 本次git使用位于自己的个人PC上,所以将个人的Gi ...
- PeopleSoft Rich Text Boxes上定制Tool Bars
在使用PT8.50或在8.51时,你可能遇到过Rich-text编辑框.该插件使你能够格式化文本,添加颜色.链接.图片等等.下面是效果图: 如果页面中只有这么一个字段,该文本框就会有足够的空间来容 ...