169 lines
5.2 KiB
Objective-C
169 lines
5.2 KiB
Objective-C
//
|
|
// NSDate+Common.m
|
|
// TIMChat
|
|
//
|
|
// Created by AlexiChen on 16/3/16.
|
|
// Copyright © 2016年 AlexiChen. All rights reserved.
|
|
//
|
|
|
|
#import "NSDate+Common.h"
|
|
#import "NSDateDeal.h"
|
|
|
|
@implementation NSDate (Common)
|
|
|
|
#define kMinuteTimeInterval (60)
|
|
#define kHourTimeInterval (60 * kMinuteTimeInterval)
|
|
#define kDayTimeInterval (24 * kHourTimeInterval)
|
|
#define kWeekTimeInterval (7 * kDayTimeInterval)
|
|
#define kMonthTimeInterval (30 * kDayTimeInterval)
|
|
#define kYearTimeInterval (12 * kMonthTimeInterval)
|
|
|
|
- (BOOL)isToday
|
|
{
|
|
NSCalendar *cal = [NSCalendar currentCalendar];
|
|
NSDateComponents *components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
|
|
NSDate *today = [cal dateFromComponents:components];
|
|
|
|
components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:self];
|
|
NSDate *otherDate = [cal dateFromComponents:components];
|
|
|
|
if([today isEqualToDate:otherDate])
|
|
{
|
|
return YES;
|
|
}
|
|
return NO;
|
|
}
|
|
|
|
- (BOOL)isYesterday
|
|
{
|
|
NSCalendar *cal = [NSCalendar currentCalendar];
|
|
NSDateComponents *components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
|
|
NSDate *today = [cal dateFromComponents:components];
|
|
NSDate *yesterday = [today dateByAddingTimeInterval: -kDayTimeInterval];
|
|
|
|
components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:self];
|
|
NSDate *otherDate = [cal dateFromComponents:components];
|
|
|
|
if([yesterday isEqualToDate:otherDate])
|
|
{
|
|
return YES;
|
|
}
|
|
return NO;
|
|
|
|
}
|
|
|
|
- (NSString *)shortTime
|
|
{
|
|
NSDate *date = self;
|
|
|
|
NSTimeInterval interval = [date timeIntervalSince1970];
|
|
|
|
return [NSDateDeal showChatListFormateTime:interval];
|
|
}
|
|
|
|
- (NSString *)shortTimeTextOfDate
|
|
{
|
|
NSDate *date = self;
|
|
|
|
NSTimeInterval interval = [date timeIntervalSince1970];
|
|
|
|
return [NSDateDeal showChatRoomFormateTime:interval];
|
|
}
|
|
|
|
- (NSString *)timeTextOfDate
|
|
{
|
|
NSDate *date = self;
|
|
|
|
NSTimeInterval interval = [date timeIntervalSinceDate:[NSDate date]];
|
|
|
|
interval = -interval;
|
|
|
|
// 今天的消息
|
|
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
|
|
[dateFormat setDateFormat:@"aHH:mm"];
|
|
[dateFormat setAMSymbol:@"上午"];
|
|
[dateFormat setPMSymbol:@"下午"];
|
|
NSString *dateString = [dateFormat stringFromDate:date];
|
|
|
|
if ([date isToday])
|
|
{
|
|
// 今天的消息
|
|
return dateString;
|
|
}
|
|
else if ([date isYesterday])
|
|
{
|
|
// 昨天
|
|
return [NSString stringWithFormat:@"昨天 %@", dateString];
|
|
}
|
|
else if (interval < kWeekTimeInterval)
|
|
{
|
|
// 最近一周
|
|
// 实例化一个NSDateFormatter对象
|
|
NSDateFormatter* weekFor = [[NSDateFormatter alloc] init];
|
|
weekFor.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
|
|
[weekFor setDateFormat:@"ccc"];
|
|
NSString *weekStr = [weekFor stringFromDate:date];
|
|
return [NSString stringWithFormat:@"%@ %@", weekStr, dateString];
|
|
}
|
|
else
|
|
{
|
|
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];
|
|
|
|
NSDateComponents *today = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]];
|
|
|
|
if ([components year] == [today year])
|
|
{
|
|
// 今年
|
|
NSDateFormatter *mdFor = [[NSDateFormatter alloc] init];
|
|
[mdFor setDateFormat:@"MM-dd"];
|
|
NSString *mdStr = [mdFor stringFromDate:date];
|
|
return [NSString stringWithFormat:@"%@ %@", mdStr, dateString];
|
|
}
|
|
else
|
|
{
|
|
// 往年
|
|
NSDateFormatter *ymdFormat = [[NSDateFormatter alloc] init];
|
|
[ymdFormat setDateFormat:@"yy-MM-dd"];
|
|
NSString *ymdString = [ymdFormat stringFromDate:date];
|
|
return [NSString stringWithFormat:@"%@ %@", ymdString, dateString];;
|
|
|
|
}
|
|
}
|
|
return nil;
|
|
}
|
|
+ (NSString *) compareCurrentTime:(long long)timestamp;
|
|
{
|
|
NSDate *timeDate = [NSDate dateWithTimeIntervalSince1970:timestamp];
|
|
NSDate *currentDate = [NSDate date];
|
|
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:timeDate];
|
|
long temp = 0;
|
|
NSString *result;
|
|
if (timeInterval/60 < 1)
|
|
{
|
|
result = [NSString stringWithFormat:@"刚刚"];
|
|
}
|
|
else if((temp = timeInterval/60) <60){
|
|
result = [NSString stringWithFormat:@"%ld分钟前",temp];
|
|
}
|
|
else if((temp = temp/60) <24){
|
|
result = [NSString stringWithFormat:@"%ld小时前",temp];
|
|
}
|
|
else if((temp = temp/24) <30){
|
|
result = [NSString stringWithFormat:@"%ld天前",temp];
|
|
}
|
|
else if((temp = temp/30) <12){
|
|
result = [NSString stringWithFormat:@"%ld月前",temp];
|
|
}
|
|
else{
|
|
temp = temp/12;
|
|
result = [NSString stringWithFormat:@"%ld年前",temp];
|
|
}
|
|
return result;
|
|
}
|
|
+ (NSString *) momentTimeFormat:(long long)timestamp
|
|
{
|
|
return [NSDateDeal showDefalutFormateTime:timestamp];
|
|
}
|
|
|
|
@end
|