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) ...
随机推荐
- html3秒跳转
<script> setTimeout( 'window.location= "home.jsp " ',3000) ;//注意,此处“;”可加可不加</ ...
- MSBI--enlarge the DW database table volume
我们在学习MSBI的时候,经常会使用官方提供的Adventureworks和AdventureworksDW示例数据库,但是官方提供的数据量有点小, 以DW为例,Factinternetsales只有 ...
- java方法可变参数的写法
jdk1.5之后出现的,该写法避免了当有多个不同个数的参数方法时,对方法的重载.其实就是数组. package com.shipin; /** * @author QiaoJiafei * @vers ...
- css3的媒体查询(Media Queries)
我今天就总结一下响应式设计的核心CSS技术Media(媒体查询器)的用法. 先看一个简单的例子: <link rel="stylesheet" media="scr ...
- python中如何将str转换成dict
>>>user "{'a':'b'}" >>>b = eval(user) >>>b {'a':'b'}
- mysql创建表
说明:此文件包含了blog数据库中建立所有的表的Mysql语句. 在sql语句中注意“约束的概念": 1.实体完整性约束(主键--唯一且非空) primary key() 违约处理:No a ...
- Lambda表达式关于like问题(未解决)
参考文章: http://stackoverflow.com/questions/3616215/like-in-lambda-expression-and-linq 1. c=>c.name. ...
- 第二章 时间控件(DateTime Picker)
这家伙太懒了,碰到问题才写博文,嘿嘿. 好了进入正题,二话不说,先放地址: 中文:http://www.bootcss.com/p/bootstrap-datetimepicker/index.htm ...
- poj3984迷宫问题 广搜+最短路径+模拟队列
转自:http://blog.csdn.net/no_retreats/article/details/8146585 定义一个二维数组: int maze[5][5] = { 0, 1, 0, ...
- Virtualbox下Ubuntu与主机Win7共享文件夹
记下来,免得老google. 1. 在虚拟机设置里设置好win7的共享文件夹位置:如c:\share 2.确定ubuntu下需要共享的文件夹,如~/linuxshare.注意,此文件夹名字必须与win ...