- (NSString *)compareDate:(NSDate *)date{

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 判断日期是今天,昨天还是明天的更多相关文章

  1. iOS判断日期A是否在日期B到日期C之间

    方法一: 可以用nsdate 的 timeIntervalSince1970 方法把时间转换成时间戳进行比较,这里timeIntervalSince1970返回的是NSTimeInterval(dou ...

  2. ios 对日期的处理(包括计算昨天时间、明天时间)

    NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. 如 ...

  3. linux中用shell获取昨天、明天或多天前的日期

    linux中用shell获取昨天.明天或多天前的日期 时间 -- :: BlogJava-专家区 原文 http://www.blogjava.net/xzclog/archive/2015/12/0 ...

  4. ios入门之c语言篇——基本函数——3——判断日期是一年的第几天

    3.判断日期是一年的第几天 参数返回值解析: 参数: y:int,年份: m:int,月份 d:int,日期 返回值: sum:传入日期是当年的第几天: 函数解析: leapyear(y);判断y是不 ...

  5. ios开发日期的NSDate,NSCalendar分类

    #import <Foundation/Foundation.h> @interface NSDate (XMGExtension) /** */ // @property (nonato ...

  6. iOS下日期的处理

    NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates         NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是 ...

  7. iOS下日期的处理(世界标准时转本地时间)

    NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates         NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是 ...

  8. JS判断日期是否在同一个星期内,和同一个月内

    今天要用到判断日期是否在同一个星期内和是否在同一个月内,在网上找了好一会儿也没找到合适的,然后自己写了一个方法来处理这个问题,思路就不详细介绍了,直接附上代码,自己测试了一下 没有问题,若有问题请在评 ...

  9. iOS 判断数组是否为空

    有人说可以用([array count]==0 )来判断是否为空,都是坑,如果array为空的话,执行count就会直接报错,程序崩溃退出. 正确判断NSArray是否为空的方法:用 (!array) ...

随机推荐

  1. Azure 上为Liunx VM 挂载File类型的存储。

    1. Create a storage account in Azure, copy the storage account endpoint URL (with postfix of "f ...

  2. stm32时钟分析

    转载自http://blog.chinaunix.net/uid-21658993-id-3129667.html   在STM32中,有五个时钟源,为HSI.HSE.LSI.LSE.PLL. 其实是 ...

  3. 解决客户端通过zookeeper连接到hbase时连接过多的问题

    原因:客户端程序通过zookeeper访问hbase的连接数超过设置的默认链接数(默认数是30),连接数不够用会导致后续的连接连接不上去. 解决办法:设置hbase-site.xml配置文件,添加如下 ...

  4. cnblogs美化及插件

    1.vp计数 http://www.amazingcounters.com 2.来源地图 http://clustrmaps.com 2.1来源地图 http://www.flagcounter.co ...

  5. FPGA 设计技巧

    1.  资源共享的应用限制在同一个module里 这样 综合工具才能最大限度地发挥其资源共 享综合作用 2.  尽可能将Critical path上所有相关逻辑放在同一个module里 这样 综合工具 ...

  6. 06-图2 Saving James Bond - Easy Version

    题目来源:http://pta.patest.cn/pta/test/18/exam/4/question/625 This time let us consider the situation in ...

  7. [转载]ExtJs4 笔记(8) Ext.slider 滚轴控件、 Ext.ProgressBar 进度条控件、 Ext.Editor 编辑控件

    作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有.转载时须注明本文的详细链接,否则作者将保留追究其法律 ...

  8. codeforces 477A A. Dreamoon and Sums(数学)

    题目链接: A. Dreamoon and Sums time limit per test 1.5 seconds memory limit per test 256 megabytes input ...

  9. leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?

    Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

  10. python中property干什么用的?

    先来段官方文档压压惊.. property(fget=None, fset=None, fdel=None, doc=None) Return a property attribute. fget i ...