NSDate+YOHO.m
2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// 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