zoukankan      html  css  js  c++  java
  • 计算时间

    NSDate+MJ.h

    #import <Foundation/Foundation.h>
    
    @interface NSDate (MJ)
    /**
     *  是否为今天
     */
    - (BOOL)isToday;
    /**
     *  是否为昨天
     */
    - (BOOL)isYesterday;
    /**
     *  是否为今年
     */
    - (BOOL)isThisYear;
    
    /**
     *  返回一个只有年月日的时间
     */
    - (NSDate *)dateWithYMD;
    
    /**
     *  获得与当前时间的差距
     */
    - (NSDateComponents *)deltaWithNow;
    @end

    NSDate+MJ.m

    //
    //  NSDate+MJ.m
    //  ItcastWeibo
    //
    //  Created by apple on 14-5-9.
    //  Copyright (c) 2014年 itcast. All rights reserved.
    //
    
    #import "NSDate+MJ.h"
    
    @implementation NSDate (MJ)
    /**
     *  是否为今天
     */
    - (BOOL)isToday
    {
        NSCalendar *calendar = [NSCalendar currentCalendar];
        int unit = NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear;
        
        // 1.获得当前时间的年月日
        NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
        
        // 2.获得self的年月日
        NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
        return
        (selfCmps.year == nowCmps.year) &&
        (selfCmps.month == nowCmps.month) &&
        (selfCmps.day == nowCmps.day);
    }
    
    /**
     *  是否为昨天
     */
    - (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;
    }
    
    - (NSDate *)dateWithYMD
    {
        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
        fmt.dateFormat = @"yyyy-MM-dd";
        NSString *selfStr = [fmt stringFromDate:self];
        return [fmt dateFromString:selfStr];
    }
    
    /**
     *  是否为今年
     */
    - (BOOL)isThisYear
    {
        NSCalendar *calendar = [NSCalendar currentCalendar];
        int unit = NSCalendarUnitYear;
        
        // 1.获得当前时间的年月日
        NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
        
        // 2.获得self的年月日
        NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
        
        return nowCmps.year == selfCmps.year;
    }
    
    - (NSDateComponents *)deltaWithNow
    {
        NSCalendar *calendar = [NSCalendar currentCalendar];
        int unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
        return [calendar components:unit fromDate:self toDate:[NSDate date] options:0];
    }
    @end
    // 创建格式化类
        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
        // 设置格式      _created_at = @"Mon Jul 14 16:13:07 +0800 2014";
        fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
        
    #warning 真机调试(真机显示时间,要写上这句话)
        fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
        // 获得发微博的具体时间
        NSDate *createDate = [fmt dateFromString:_created_at];
        
        // 判断是否为今年
        if (!createDate.isThisYear) {
            fmt.dateFormat = @"yyyy-MM-dd";
            return [fmt stringFromDate:createDate];
        }
        
        if (!createDate.isToday && !createDate.isYesterday) { // 不是今天,也不是昨天
                fmt.dateFormat = @"MM-dd HH:mm";
                return [fmt stringFromDate:createDate];
        }
        if (!createDate.isToday) {
            fmt.dateFormat = @"昨天 HH:mm";
            return [fmt stringFromDate:createDate];
        }
        NSDateComponents *cmps = [createDate deltaWithNow];
        
        if (!cmps.hour >= 1 && !cmps.minute >= 1) {
             return @"刚刚";
        }
        
        if (!cmps.hour >= 1) {
            return [NSString stringWithFormat:@"%d分钟前", cmps.minute];
        }
        
        return  [NSString stringWithFormat:@"%d小时前", cmps.hour];
  • 相关阅读:
    Scala控制抽象
    【转】ZooKeeper详细介绍和使用第一节
    zookeeper入门系列讲解
    最全面的 Spring 学习笔记
    MySQL 字符串拼接详解
    细说Python2.x与3​​.x版本区别
    【转】微信公众开发URL和token填写详解
    【转】Java代码操作Redis的sentinel和Redis的集群Cluster操作
    Java正则表达式的使用和详解(下)
    Java正则表达式的使用和详解(上)
  • 原文地址:https://www.cnblogs.com/Fc-ios/p/3920215.html
Copyright © 2011-2022 走看看