• NSDate分类,把系统返回的时间与现在进行比较---秀清


    //
    //  NSDate+Joe.h
    //  WeiBo
    //
    //  Created by 张秀清 on 15/9/17.
    //  Copyright (c) 2015年 张秀清. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface NSDate (Joe)
    
    /**
     *  是否是今天
     */
    -(BOOL)isToday;
    /**
     *  是否是这个月
     */
    -(BOOL)isThisYear;
    /**
     *  是否是今年
     */
    -(BOOL)isThisMonth;
    /**
     *  是否为昨天
     */
    - (BOOL)isYesterday;
    
    /**
     *  返回'YYYY-MM-DD'的nsdate
     */
    - (NSDate *)dateWithYMD;
    
    /**
     *  返回与当前时间的差距
     */
    - (NSDateComponents *)deltaWithNow;
    @end
    
    
    
    //
    //  NSDate+Joe.m
    //  WeiBo
    //
    //  Created by 张秀清 on 15/9/17.
    //  Copyright (c) 2015年 张秀清. All rights reserved.
    //
    
    #import "NSDate+Joe.h"
    
    @implementation NSDate (Joe)
    
    -(BOOL)isToday
    {
        NSCalendar *calendar = [NSCalendar currentCalendar];
        
        int NSCalendarUnit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
        
        //获取当前的年月日
        NSDateComponents *nowCps = [calendar components:NSCalendarUnit fromDate:[NSDate date]];
        
        NSDateComponents *selfCps = [calendar components:NSCalendarUnit fromDate:self];
        
        return (nowCps.year == selfCps.year) && (nowCps.month == selfCps.month) && (nowCps.day == selfCps.day);
    }
    
    -(BOOL)isThisMonth
    {
        NSCalendar *calendar = [NSCalendar currentCalendar];
        
        int NSCalendarUnit = NSCalendarUnitYear | NSCalendarUnitMonth;
        
        //获取当前的年月日
        NSDateComponents *nowCps = [calendar components:NSCalendarUnit fromDate:[NSDate date]];
        
        NSDateComponents *selfCps = [calendar components:NSCalendarUnit fromDate:self];
        
        return (nowCps.year == selfCps.year) && (nowCps.month == selfCps.month);
    
    }
    
    -(BOOL)isThisYear
    {
        NSCalendar *calendar = [NSCalendar currentCalendar];
        
        int NSCalendarUnit = NSCalendarUnitYear;
        
        //获取当前的年月日
        NSDateComponents *nowCps = [calendar components:NSCalendarUnit fromDate:[NSDate date]];
        
        NSDateComponents *selfCps = [calendar components:NSCalendarUnit fromDate:self];
        
        return nowCps.year == selfCps.year;
    }
    
    /**
     *  是否为昨天
     */
    - (BOOL)isYesterday
    {
        // 2014-05-01
        NSDate *nowDate = [[NSDate date] dateWithYMD];
        
        // 2014-04-30
        NSDate *selfDate = [self dateWithYMD];
        
        // 获得nowDate和selfDate的差距
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *cmps = [calendar components:NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];
        return cmps.day == 1;
    }
    
    /**
     *  返回'YYYY-MM-DD'的nsdate
     */
    - (NSDate *)dateWithYMD
    {
        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
        fmt.dateFormat = @"yyyy-MM-dd";
        NSString *selfStr = [fmt stringFromDate:self];
        return [fmt dateFromString:selfStr];
    }
    
    /**
     *  返回与当前时间的差距
     */
    - (NSDateComponents *)deltaWithNow
    {
        NSCalendar *calendar = [NSCalendar currentCalendar];
        int unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
        return [calendar components:unit fromDate:self toDate:[NSDate date] options:0];
    }
    @end
  • 相关阅读:
    【转】灰色在PPT中的运用
    [转]多多“亦”善:把大量内容放到一页PPT的5个技巧
    android应用程序的安装方式与原理
    oracle 数据库 date + 1 转载
    运行ant脚本(转载)
    Oracle修改字段类型方法总结(转)
    转载 jQueryEasyUI Messager基本使用
    oracle dmp数据导入
    oracle 查看表的索引信息
    sqlplus 分析执行计划
  • 原文地址:https://www.cnblogs.com/sixindev/p/4819949.html
Copyright © 2020-2023  润新知