NSDate+YOHO.m 2.44 KB
//
//  NSDate+YOHO.m
//  YH_Mall
//
//  Created by Haizi on 16/2/23.
//  Copyright © 2016年 YOHO. All rights reserved.
//

#import "NSDate+YOHO.h"

NSTimeInterval const kTimeIntervalHour = 60*60;
NSTimeInterval const kTimeIntervalDay = 60*60*24;

@implementation NSDate (YOHO)

static NSDateFormatter *formatter_ = nil;
- (NSString *)yh_stringRepresentationWithDateFormat:(NSString *)format
{
    formatter_ = [[NSDateFormatter alloc] init];
    NSString *dateFormat = nil;
    if (format == nil) {
        dateFormat = @"yyyy.MM.dd HH: mm: ss";
    } else {
        dateFormat = format;
    }
    formatter_.dateFormat = dateFormat;
    NSString *dateString = [formatter_ stringFromDate:self];
    return dateString;
}

+ (NSString *)yh_dateIntervalStringWithInterval:(NSTimeInterval)paramInterval
{
    NSString *intervalString = nil;
    
    NSDate *currentDate = [NSDate date]; //当前时间
    NSDate *paramDate = [NSDate dateWithTimeIntervalSince1970:paramInterval]; //参数时间戳标示的时间
    
    NSCalendar *gregorianCanlendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    NSDateComponents *currentComponents = [gregorianCanlendar components:unitFlags fromDate:currentDate];
    NSDateComponents *paramComponents = [gregorianCanlendar components:unitFlags fromDate:paramDate];
    NSDateComponents *yesterdayComponents = [gregorianCanlendar components:unitFlags fromDate:[NSDate dateWithTimeIntervalSinceNow:(86400 * 1)] ];
    
    if((currentComponents.year == paramComponents.year) && (currentComponents.month == paramComponents.month) && (currentComponents.day == paramComponents.day)) {
        intervalString = [NSString stringWithFormat:NSLocalizedString(@"today time", nil),(long)paramComponents.hour,(long)paramComponents.minute];
        
    }else if ((yesterdayComponents.year == paramComponents.year) && (yesterdayComponents.month == paramComponents.month) && (yesterdayComponents.day == paramComponents.day)){
        
        intervalString = [NSString stringWithFormat:NSLocalizedString(@"yesterday time", nil),(long)paramComponents.hour,(long)paramComponents.minute];
    }else{
        intervalString = [NSString stringWithFormat:@"%04ld.%ld.%ld",(long)paramComponents.year,(long)paramComponents.month,(long)paramComponents.day];
    }
    return intervalString;
}

@end