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) ...
随机推荐
- tomcat发布记录
web项目发布详细步骤 服务器 tomcat服务器1.删除webapps文件夹里面的项目war包-->ifm.war(项目war名称)2.把项目的ifm.war放到webapps里面3.删除we ...
- C语言使用宏实现2个变量的交换
记录哪个方法更普适,更高效,这些方法不包括使用函数的方法,如果使用函数的话,使用指针的方法更合适. 使用中间变量 形如 int tmp, tmp = a; a=b; b = tmp; #define ...
- Sql Practice 2
之前写了一个SP用来向dimention table插入0 -1 dummy row的值,但今天在process adventureworksdw2008示例 数据库的时候报错,查看了一下,是因为自己 ...
- uva 10305 ordering tasks(超级烂题)——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABHIAAAHDCAYAAABI5T2bAAAgAElEQVR4nOydPY7svLW1awQGNABHCm
- C++ macro(宏)使用小结
谈起C++中的宏,我们第一个想到的应该就是“#define”,它的基本语法长得像这样: #define macroname(para1, para2, para3, ... ,paran) macro ...
- AD批量创建用户
实验环境:Windows Server 2008R 2 由于测试需要,需要创建数百个用户,手动创建当然不可取,此时需要批量创建,操作记录如下 1 首先将要批量创建的人员信息导入到一个csv文件中,表中 ...
- 一个CentOS7的开发环境部署,包括防火墙|VPN|多IP多网关|HTTP代理服务器设置等
http://www.lenggirl.com/code/centos7.html layout: post title: "一个CentOS7的开发环境部署,包括防火墙|VPN|HTTP代 ...
- C/C++学习----使用C语言代替cmd命令、cmd命令大全
[开发环境] 物理机版本:Win 7 旗舰版(64位) IDE版本:Visual Studio 2013简体中文旗舰版(cn_visual_studio_ultimate_2013_with_upda ...
- sqlzoo.net刷题3
Find the continents where all countries have a population <= 25000000. Then find the names of the ...
- ASP.NET MVC 扩展数据验证 转
此文只作记录 public class MaxWordsAttribute : ValidationAttribute { public MaxWordsAttribute(int maxWords) ...