IOS 判断日期是今天,昨天还是明天
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *today = [[NSDate alloc] init];
NSDate *tomorrow, *yesterday;
tomorrow = [today dateByAddingTimeInterval: secondsPerDay];
yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
// 10 first characters of description is the calendar date:
NSString * todayString = [[today description] substringToIndex:10];
NSString * yesterdayString = [[yesterday description] substringToIndex:10];
NSString * tomorrowString = [[tomorrow description] substringToIndex:10];
NSString * dateString = [[date description] substringToIndex:10];
if ([dateString isEqualToString:todayString])
{
return @"今天";
} else if ([dateString isEqualToString:yesterdayString])
{
return @"昨天";
}else if ([dateString isEqualToString:tomorrowString])
{
return @"明天";
}
else
{
return dateString;
}
}
///// 和当前时间比较
//// 1)1分钟以内 显示 : 刚刚
//// 2)1小时以内 显示 : X分钟前
/// 3)今天或者昨天 显示 : 今天 09:30 昨天 09:30
/// 4) 今年显示 : 09月12日
/// 5) 大于本年 显示 : 2013/09/09
**/
+ (NSString *)formateDate:(NSString *)dateString withFormate:(NSString *) formate
{
@try {
//实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formate];
NSDate * nowDate = [NSDate date];
///// 将需要转换的时间转换成 NSDate 对象
NSDate * needFormatDate = [dateFormatter dateFromString:dateString];
///// 取当前时间和转换时间两个日期对象的时间间隔
///// 这里的NSTimeInterval 并不是对象,是基本型,其实是double类型,是由c定义的: typedef double NSTimeInterval;
NSTimeInterval time = [nowDate timeIntervalSinceDate:needFormatDate];
//// 再然后,把间隔的秒数折算成天数和小时数:
NSString *dateStr = @"";
if (time<=60) { //// 1分钟以内的
dateStr = @"刚刚";
}else if(time<=60*60){ //// 一个小时以内的
int mins = time/60;
dateStr = [NSString stringWithFormat:@"%d分钟前",mins];
}else if(time<=60*60*24){ //// 在两天内的
[dateFormatter setDateFormat:@"YYYY/MM/dd"];
NSString * need_yMd = [dateFormatter stringFromDate:needFormatDate];
NSString *now_yMd = [dateFormatter stringFromDate:nowDate];
[dateFormatter setDateFormat:@"HH:mm"];
if ([need_yMd isEqualToString:now_yMd]) {
//// 在同一天
dateStr = [NSString stringWithFormat:@"今天 %@",[dateFormatter stringFromDate:needFormatDate]];
}else{
//// 昨天
dateStr = [NSString stringWithFormat:@"昨天 %@",[dateFormatter stringFromDate:needFormatDate]];
}
}else {
[dateFormatter setDateFormat:@"yyyy"];
NSString * yearStr = [dateFormatter stringFromDate:needFormatDate];
NSString *nowYear = [dateFormatter stringFromDate:nowDate];
if ([yearStr isEqualToString:nowYear]) {
//// 在同一年
[dateFormatter setDateFormat:@"MM月dd日"];
dateStr = [dateFormatter stringFromDate:needFormatDate];
}else{
[dateFormatter setDateFormat:@"yyyy/MM/dd"];
dateStr = [dateFormatter stringFromDate:needFormatDate];
}
}
return dateStr;
}
@catch (NSException *exception) {
return @"";
}
}
IOS 判断日期是今天,昨天还是明天的更多相关文章
- iOS判断日期A是否在日期B到日期C之间
方法一: 可以用nsdate 的 timeIntervalSince1970 方法把时间转换成时间戳进行比较,这里timeIntervalSince1970返回的是NSTimeInterval(dou ...
- ios 对日期的处理(包括计算昨天时间、明天时间)
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. 如 ...
- linux中用shell获取昨天、明天或多天前的日期
linux中用shell获取昨天.明天或多天前的日期 时间 -- :: BlogJava-专家区 原文 http://www.blogjava.net/xzclog/archive/2015/12/0 ...
- ios入门之c语言篇——基本函数——3——判断日期是一年的第几天
3.判断日期是一年的第几天 参数返回值解析: 参数: y:int,年份: m:int,月份 d:int,日期 返回值: sum:传入日期是当年的第几天: 函数解析: leapyear(y);判断y是不 ...
- ios开发日期的NSDate,NSCalendar分类
#import <Foundation/Foundation.h> @interface NSDate (XMGExtension) /** */ // @property (nonato ...
- iOS下日期的处理
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是 ...
- iOS下日期的处理(世界标准时转本地时间)
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是 ...
- JS判断日期是否在同一个星期内,和同一个月内
今天要用到判断日期是否在同一个星期内和是否在同一个月内,在网上找了好一会儿也没找到合适的,然后自己写了一个方法来处理这个问题,思路就不详细介绍了,直接附上代码,自己测试了一下 没有问题,若有问题请在评 ...
- iOS 判断数组是否为空
有人说可以用([array count]==0 )来判断是否为空,都是坑,如果array为空的话,执行count就会直接报错,程序崩溃退出. 正确判断NSArray是否为空的方法:用 (!array) ...
随机推荐
- splice()函数,'SPLICE_F_MOVE' 'SPLICE_F_NONBLOCK' 'SPLICE_F_MORE' undeclared
1.编译含有splice()函数的程序时出现,'SPLICE_F_MOVE' undeclared,'SPLICE_F_NONBLOCK' ‘SPLICE_F_MORE' 也是一样undeclare ...
- Error during installing HAXM, VT-X not working 在安装HAXM错误,开始不工作
最佳答案 (Best Answer) Some antivirus options prevent Haxm installation. ie: Avast : settings (parametre ...
- DB String Split sample
以sqlserver为例进行说明,代码稍加修改,可以用于ORACLE. 字符串的拆分和聚合是数据库开发人员比较常见的任务. 关于字符串的聚合和拆分,sqlserver的标准解决方案是for xml p ...
- [转]Oracle分页之二:自定义web分页控件的封装
本文转自:http://www.cnblogs.com/scy251147/archive/2011/04/16/2018326.html 上节中,讲述的就是Oracle存储过程分页的使用方式,但是如 ...
- Mysql如何清空数据库的所有表数据
1.先查询出库中的所有表,“db”是数据库名称 SELECT CONCAT('truncate table ',TABLE_NAME,';') AS a FROM INFORMATION_SCHEMA ...
- Dubbo架构设计详解(转自shiyanjun.cn)
Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合).从服务模型的角度来看,Dubbo采用的是一种非常简单的模 ...
- make命令--基础
一.Make的概念 Make这个词,英语的意思是"制作".Make命令直接用了这个意思,就是要做出某个文件.比如,要做出文件a.txt,就可以执行下面的命令. $ make a.t ...
- POJ 2891 Strange Way to Express Integers【扩展欧几里德】【模线性方程组】
求解方程组 X%m1=r1 X%m2=r2 .... X%mn=rn 首先看下两个式子的情况 X%m1=r1 X%m2=r2 联立可得 m1*x+m2*y=r2-r1 用ex_gcd求得一个特解x' ...
- RabbitMQ 一二事(5) - 通配符模式应用
之前的路由模式是通过key相等来匹配 而通配符,顾名思义,符合条件,则进行消息匹配发送 将路由键和某模式进行匹配.此时队列需要绑定要一个模式上. 符号“#”匹配一个或多个词,符号“*”匹配不多不少一个 ...
- 每日学习心得:UEditor样式被过滤无法显示问题
前言: 上周开发中有用到开源的富文本编辑器UEditor,在使用的过程中遇到了样式被过滤无法显示问题,经过一番折腾终解决,此外,还有一些关于获取前台界面元素的一些总结. 1. UEditor样式被过滤 ...