ios 计算任意一个时间距离现在的时间差

1、两种方法

1 传统方法

NSDate *nowDate = [NSDate date]; // 当前日期
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH-mm-ss";
NSDate *creat = [formatter dateFromString:(任意时间)];// 将传入的字符串转化成时间
NSTimeInterval delta = [nowDate timeIntervalSinceDate:creat]; // 计算出相差多少秒 
//打印结果格式为 delta ==== 181078.541819

2 便捷方法

NSDate *nowDate = [NSDate date]; // 当前时间
    
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH-mm-ss";
NSDate *creat = [formatter dateFromString:creat_time]; // 传入的时间  

// 这里有个需要注意的地方,不要将时间戳的字符串直接作为creat_time ,如果是时间戳,那么
 NSString *timeString = @"1532754000"; // 时间戳字符串
NSTimeInterval time=[timeString doubleValue]/1000;//传入的时间戳str如果是精确到毫秒的记得要/1000 (注意: 这句话不要全盘复制,如果传入的时间戳没有精确到毫秒不要除1000)
NSDate *creat =[NSDate dateWithTimeIntervalSince1970:time];
    
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *compas = [calendar components:unit fromDate:creat toDate:nowDate options:0];
NSLog(@"year=%zd  month=%zd  day=%zd hour=%zd  minute=%zd",compas.year,compas.month,compas.day,compas.hour,compas.minute);

2.封装一些简单实用的方法,NSDate的分类

import "NSDate+Extension.h"

#import 

@interface NSDate (Extension)

/**
 *传入时间与当前时间的差值
 */
- (NSDateComponents *)deltaFrom:(NSDate *)date;

/**
 *是否为今年
 */
- (BOOL)isThisYear;

/**
 *是否为今天
 */
- (BOOL)isToday;

/**
 *是否为昨天
 */
- (BOOL)isYestoday;

/**
 *返回只有年月日的时间
 */
- (NSDate *)dateWithYMD;

import "NSDate+Extension.m"

#import "NSDate+Extension.h"

@implementation NSDate (Extension)

- (NSDateComponents *)deltaFrom:(NSDate *)date{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *compas = [calendar components:unit fromDate:self toDate:date options:0];
    
    return compas;
}



- (BOOL)isThisYear{
    
    // 日历
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
    NSInteger selfYear = [calendar component:NSCalendarUnitYear fromDate:self];
    return nowYear == selfYear;
}

// 第一种方法
//- (BOOL)isToday{
//    // 日历
//    NSCalendar *calendar = [NSCalendar currentCalendar];
//    NSCalendarUnit unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
//    NSDateComponents *nowComp = [calendar components:unit fromDate:[NSDate date]];
//    NSDateComponents *selfComp = [calendar components:unit fromDate:self];

//    return nowComp.year == selfComp.year
//    && nowComp.month == selfComp.month
//    && nowComp.day == selfComp.day;
//}

// 第二种方法
- (BOOL)isToday{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *nowString = [formatter stringFromDate:[NSDate date]];
    NSString *selfString = [formatter stringFromDate:self];
    return nowString == selfString;
}


- (BOOL)isYestoday{
    NSDate *nowDate = [[NSDate date] dateWithYMD];
    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 *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *selfString = [formatter stringFromDate:self];
    return [formatter dateFromString:selfString];
}



@end

你可能感兴趣的:(ios 计算任意一个时间距离现在的时间差)