333 lines
14 KiB
Objective-C
Executable File
333 lines
14 KiB
Objective-C
Executable File
//
|
|
// NSDateDeal.m
|
|
// moneyshield
|
|
//
|
|
// Created by striveliu on 15/6/24.
|
|
// Copyright (c) 2015年 Alibaba.inc. All rights reserved.
|
|
//
|
|
|
|
#import "NSDateDeal.h"
|
|
|
|
@implementation NSDateDeal
|
|
|
|
+ (int)dateInterval:(NSDate *)aStartDate endDate:(NSDate *)aEndDate
|
|
{
|
|
//#ifdef DEBUG
|
|
// if (fabs([aStartDate timeIntervalSinceDate:aEndDate]) > 0.2*60)
|
|
// {
|
|
// return YES;
|
|
// }
|
|
//#else
|
|
if (fabs([aStartDate timeIntervalSinceDate:aEndDate]) > 2*60*60)
|
|
{
|
|
return YES;
|
|
}
|
|
//#endif
|
|
return 0;
|
|
}
|
|
|
|
+ (int)getDateInterval:(NSDate *)aStartDate endDate:(NSDate *)aEndDate
|
|
{
|
|
return fabs([aStartDate timeIntervalSinceDate:aEndDate]);
|
|
}
|
|
#pragma clang diagnostic push
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
|
|
+ (NSInteger)getCurrentYear
|
|
{
|
|
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
|
|
NSDate *now;
|
|
NSDateComponents *comps = [[NSDateComponents alloc] init];
|
|
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
|
|
now=[NSDate date];
|
|
comps = [calendar components:unitFlags fromDate:now];
|
|
NSInteger year = [comps year];
|
|
return year;
|
|
}
|
|
|
|
+ (NSInteger)getCurrentMonth
|
|
{
|
|
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
|
|
NSDate *now;
|
|
NSDateComponents *comps = [[NSDateComponents alloc] init];
|
|
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
|
|
now=[NSDate date];
|
|
comps = [calendar components:unitFlags fromDate:now];
|
|
NSInteger month = [comps month];
|
|
return month;
|
|
}
|
|
|
|
+ (NSInteger)getCurrentDay
|
|
{
|
|
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
|
|
NSDate *now;
|
|
NSDateComponents *comps = [[NSDateComponents alloc] init];
|
|
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
|
|
now=[NSDate date];
|
|
comps = [calendar components:unitFlags fromDate:now];
|
|
NSInteger month = [comps day];
|
|
return month;
|
|
}
|
|
|
|
+ (NSInteger)getYear:(long long)aTimerInterval
|
|
{
|
|
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
|
|
NSDate* date = [NSDate dateWithTimeIntervalSince1970:aTimerInterval];
|
|
|
|
NSDateComponents *comps = [[NSDateComponents alloc] init];
|
|
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
|
|
comps = [calendar components:unitFlags fromDate:date];
|
|
return [comps year];
|
|
}
|
|
|
|
+ (NSInteger)getMonth:(long long)aTimerInterval
|
|
{
|
|
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
|
|
NSDate* date = [NSDate dateWithTimeIntervalSince1970:aTimerInterval];
|
|
|
|
NSDateComponents *comps = [[NSDateComponents alloc] init];
|
|
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
|
|
comps = [calendar components:unitFlags fromDate:date];
|
|
return [comps month];
|
|
}
|
|
|
|
+ (NSInteger)getDay:(long long)aTimerInterval
|
|
{
|
|
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
|
|
NSDate* date = [NSDate dateWithTimeIntervalSince1970:aTimerInterval];
|
|
|
|
NSDateComponents *comps = [[NSDateComponents alloc] init];
|
|
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
|
|
comps = [calendar components:unitFlags fromDate:date];
|
|
return [comps day];
|
|
}
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
+ (NSString *)formateTimerInterval:(long long)aTimerInterval formate:(NSString *)aFormate
|
|
{
|
|
NSDate* date = [NSDate dateWithTimeIntervalSince1970:aTimerInterval];
|
|
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
|
|
[dateFormatter setDateFormat:aFormate];
|
|
return [dateFormatter stringFromDate:date];
|
|
}
|
|
|
|
+ (NSString*)showTime:(NSTimeInterval) msglastTime showDetail:(BOOL)showDetail
|
|
{
|
|
//今天的时间
|
|
NSDate * nowDate = [NSDate date];
|
|
NSDate * msgDate = [NSDate dateWithTimeIntervalSince1970:msglastTime];
|
|
NSString *result = nil;
|
|
NSCalendarUnit components = (NSCalendarUnit)(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitHour | NSCalendarUnitMinute);
|
|
NSDateComponents *nowDateComponents = [[NSCalendar currentCalendar] components:components fromDate:nowDate];
|
|
NSDateComponents *msgDateComponents = [[NSCalendar currentCalendar] components:components fromDate:msgDate];
|
|
|
|
NSInteger hour = msgDateComponents.hour;
|
|
NSTimeInterval gapTime = -msgDate.timeIntervalSinceNow;
|
|
double onedayTimeIntervalValue = 24*60*60; //一天的秒数
|
|
result = [NSDateDeal getPeriodOfTime:hour withMinute:msgDateComponents.minute];
|
|
if (hour > 12)
|
|
{
|
|
hour = hour - 12;
|
|
}
|
|
if (gapTime < onedayTimeIntervalValue * 3 && gapTime > 0) {
|
|
int gapDay = gapTime/(60*60*24) ;
|
|
if(gapDay == 0) //在24小时内,存在跨天的现象. 判断两个时间是否在同一天内
|
|
{
|
|
BOOL isSameDay = msgDateComponents.day == nowDateComponents.day;
|
|
result = isSameDay ? [[NSString alloc] initWithFormat:@"%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : (showDetail? [[NSString alloc] initWithFormat:@"昨天%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : @"昨天");
|
|
}
|
|
else if(gapDay == 1)//昨天
|
|
{
|
|
result = showDetail? [[NSString alloc] initWithFormat:@"昨天%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : @"昨天";
|
|
}
|
|
else if(gapDay == 2) //前天
|
|
{
|
|
result = showDetail? [[NSString alloc] initWithFormat:@"前天%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : @"前天";
|
|
}
|
|
}
|
|
else if([nowDate timeIntervalSinceDate:msgDate] < 7 * onedayTimeIntervalValue && gapTime > 0)//一周内
|
|
{
|
|
NSString *weekDay = [NSDateDeal weekdayStr:msgDateComponents.weekday];
|
|
result = showDetail? [weekDay stringByAppendingFormat:@"%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : weekDay;
|
|
}
|
|
else//显示日期
|
|
{
|
|
NSString *day = [NSString stringWithFormat:@"%zd-%zd-%zd", msgDateComponents.year, msgDateComponents.month, msgDateComponents.day];
|
|
result = showDetail? [day stringByAppendingFormat:@" %@ %zd:%02d",result,hour,(int)msgDateComponents.minute]:day;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
+ (NSString *)showDefalutFormateTime:(long long)timestamp
|
|
{
|
|
// NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
|
|
// fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
|
|
// fmt.locale=[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
|
|
// NSDate *testDate = [fmt dateFromString:@"2017-12-13 09:25:10"];
|
|
// timestamp = [testDate timeIntervalSince1970];
|
|
|
|
//今天的时间
|
|
NSDate * nowDate = [NSDate date];
|
|
NSDate * date = [NSDate dateWithTimeIntervalSince1970:timestamp];
|
|
NSCalendarUnit components = (NSCalendarUnit)(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitHour | NSCalendarUnitMinute);
|
|
NSDateComponents *nowDateComponents = [[NSCalendar currentCalendar] components:components fromDate:nowDate];
|
|
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:components fromDate:date];
|
|
|
|
NSTimeInterval gapTime = -date.timeIntervalSinceNow;
|
|
|
|
if (dateComponents.year != nowDateComponents.year) // 不在同一年
|
|
return [NSString stringWithFormat:@"%zd-%zd-%zd", dateComponents.year, dateComponents.month, dateComponents.day];
|
|
|
|
double onedayTimeIntervalValue = 24*60*60; //一天的秒数
|
|
|
|
if (gapTime < onedayTimeIntervalValue * 2 && gapTime > 0) {
|
|
int gapDay = gapTime/(60*60*24) ;
|
|
if (gapDay == 0 && dateComponents.day == nowDateComponents.day) { // 今天
|
|
if (gapTime < 60) { // 1分钟内
|
|
return @"刚刚";
|
|
} else if (gapTime < 60 * 60) { // xx分钟前
|
|
return [NSString stringWithFormat:@"%d分前", (int)gapTime / 60];
|
|
} else { // 今天
|
|
return [NSString stringWithFormat:@"%02zd:%02zd", dateComponents.hour, dateComponents.minute];
|
|
}
|
|
} else { //昨天
|
|
NSDate *tmpDate = [NSDate dateWithTimeIntervalSince1970:timestamp+onedayTimeIntervalValue];
|
|
NSDateComponents *tmpDateComponents = [[NSCalendar currentCalendar] components:components fromDate:tmpDate];
|
|
if (gapDay == 0 || tmpDateComponents.day == nowDateComponents.day) {
|
|
return [NSString stringWithFormat:@"昨天 %02zd:%02d", dateComponents.hour, (int)dateComponents.minute] ;
|
|
}
|
|
}
|
|
}
|
|
return [NSString stringWithFormat:@"%zd-%zd %02zd:%02d", dateComponents.month, dateComponents.day, dateComponents.hour, (int)dateComponents.minute];
|
|
}
|
|
|
|
|
|
+ (NSString *)showChatListFormateTime:(long long)timestamp
|
|
{
|
|
//今天的时间
|
|
NSDate * nowDate = [NSDate date];
|
|
NSDate * date = [NSDate dateWithTimeIntervalSince1970:timestamp];
|
|
NSCalendarUnit components = (NSCalendarUnit)(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitHour | NSCalendarUnitMinute);
|
|
NSDateComponents *nowDateComponents = [[NSCalendar currentCalendar] components:components fromDate:nowDate];
|
|
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:components fromDate:date];
|
|
|
|
NSTimeInterval gapTime = -date.timeIntervalSinceNow;
|
|
|
|
if (dateComponents.year != nowDateComponents.year) // 不在同一年
|
|
return [NSString stringWithFormat:@"%zd-%zd-%zd", dateComponents.year, dateComponents.month, dateComponents.day];
|
|
|
|
double onedayTimeIntervalValue = 24*60*60; //一天的秒数
|
|
|
|
if (gapTime < onedayTimeIntervalValue * 2 && gapTime > 0) {
|
|
int gapDay = gapTime/(60*60*24) ;
|
|
if (gapDay == 0 && dateComponents.day == nowDateComponents.day) { // 今天
|
|
return [NSString stringWithFormat:@"%02zd:%02zd", dateComponents.hour, dateComponents.minute];
|
|
} else { //昨天
|
|
NSDate *tmpDate = [NSDate dateWithTimeIntervalSince1970:timestamp+onedayTimeIntervalValue];
|
|
NSDateComponents *tmpDateComponents = [[NSCalendar currentCalendar] components:components fromDate:tmpDate];
|
|
if (gapDay == 0 || tmpDateComponents.day == nowDateComponents.day) {
|
|
return @"昨天";
|
|
}
|
|
}
|
|
}
|
|
return [NSString stringWithFormat:@"%zd-%zd", dateComponents.month, dateComponents.day];
|
|
}
|
|
|
|
+ (NSString *)showChatRoomFormateTime:(long long)timestamp
|
|
{
|
|
//今天的时间
|
|
NSDate * nowDate = [NSDate date];
|
|
NSDate * date = [NSDate dateWithTimeIntervalSince1970:timestamp];
|
|
NSCalendarUnit components = (NSCalendarUnit)(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitHour | NSCalendarUnitMinute);
|
|
NSDateComponents *nowDateComponents = [[NSCalendar currentCalendar] components:components fromDate:nowDate];
|
|
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:components fromDate:date];
|
|
|
|
NSTimeInterval gapTime = -date.timeIntervalSinceNow;
|
|
|
|
if (dateComponents.year != nowDateComponents.year) // 不在同一年
|
|
// 2018-11-5
|
|
return [NSString stringWithFormat:@"%zd-%zd-%zd", dateComponents.year, dateComponents.month, dateComponents.day];
|
|
|
|
double onedayTimeIntervalValue = 24*60*60; //一天的秒数
|
|
|
|
if (gapTime < onedayTimeIntervalValue * 2 && gapTime > 0) {
|
|
int gapDay = gapTime/(60*60*24) ;
|
|
if (gapDay == 0 && dateComponents.day == nowDateComponents.day) { // 今天
|
|
// 15:02
|
|
return [NSString stringWithFormat:@"%02zd:%02zd", dateComponents.hour, dateComponents.minute];
|
|
} else { //昨天
|
|
NSDate *tmpDate = [NSDate dateWithTimeIntervalSince1970:timestamp+onedayTimeIntervalValue];
|
|
NSDateComponents *tmpDateComponents = [[NSCalendar currentCalendar] components:components fromDate:tmpDate];
|
|
if (gapDay == 0 || tmpDateComponents.day == nowDateComponents.day) {
|
|
// 昨天 13:30
|
|
return [[NSString alloc] initWithFormat:@"昨天 %02zd:%02d", dateComponents.hour, (int)dateComponents.minute] ;
|
|
}
|
|
}
|
|
}
|
|
// 11-5 13:30
|
|
return [NSString stringWithFormat:@"%zd-%zd %02zd:%02d", dateComponents.month, dateComponents.day, dateComponents.hour, (int)dateComponents.minute];
|
|
}
|
|
|
|
/// 显示客人态动态打卡显示
|
|
+ (NSString *)showOtherPunchFormateTime:(long long)timestamp
|
|
{
|
|
NSTimeInterval currentTimestamp = [[NSDate date] timeIntervalSince1970];
|
|
NSTimeInterval diffTimestamp = currentTimestamp - timestamp;
|
|
NSInteger diffDay = (diffTimestamp / (60 * 60 * 24));
|
|
if (diffDay < 3) {
|
|
return @"三天内";
|
|
} else if (diffDay < 7) {
|
|
return @"七天内";
|
|
} else if (diffDay < 30) {
|
|
return @"一个月内";
|
|
}
|
|
return @"一个月以前";
|
|
}
|
|
|
|
#pragma mark - Private
|
|
|
|
+ (NSString *)getPeriodOfTime:(NSInteger)time withMinute:(NSInteger)minute
|
|
{
|
|
NSInteger totalMin = time *60 + minute;
|
|
NSString *showPeriodOfTime = @"";
|
|
if (totalMin > 0 && totalMin <= 5 * 60)
|
|
{
|
|
showPeriodOfTime = @"凌晨";
|
|
}
|
|
else if (totalMin > 5 * 60 && totalMin < 12 * 60)
|
|
{
|
|
showPeriodOfTime = @"上午";
|
|
}
|
|
else if (totalMin >= 12 * 60 && totalMin <= 18 * 60)
|
|
{
|
|
showPeriodOfTime = @"下午";
|
|
}
|
|
else if ((totalMin > 18 * 60 && totalMin <= (23 * 60 + 59)) || totalMin == 0)
|
|
{
|
|
showPeriodOfTime = @"晚上";
|
|
}
|
|
return showPeriodOfTime;
|
|
}
|
|
|
|
+(NSString*)weekdayStr:(NSInteger)dayOfWeek
|
|
{
|
|
static NSDictionary *daysOfWeekDict = nil;
|
|
daysOfWeekDict = @{@(1):@"星期日",
|
|
@(2):@"星期一",
|
|
@(3):@"星期二",
|
|
@(4):@"星期三",
|
|
@(5):@"星期四",
|
|
@(6):@"星期五",
|
|
@(7):@"星期六",};
|
|
return [daysOfWeekDict objectForKey:@(dayOfWeek)];
|
|
}
|
|
|
|
+ (NSTimeInterval)getCurrentTimeInterval
|
|
{
|
|
NSDate *date = [NSDate date];
|
|
return date.timeIntervalSince1970;
|
|
}
|
|
@end
|