123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- //
- // NSDate+RQExtension.m
- // RQCommon
- //
- // Created by 张嵘 on 2018/11/23.
- // Copyright © 2018 张嵘. All rights reserved.
- //
- #import "NSDate+RQExtension.h"
- #define DATE_COMPONENTS (NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekOfMonth | NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal)
- #define CURRENT_CALENDAR [NSCalendar currentCalendar]
- @implementation NSDate (RQExtension)
- /**
- * 是否为今天
- */
- - (BOOL)rq_isToday
- {
- NSCalendar *calendar = [NSCalendar currentCalendar];
- int unit = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
-
- // 1.获得当前时间的年月日
- NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
-
- // 2.获得self的年月日
- NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
- return
- (selfCmps.year == nowCmps.year) &&
- (selfCmps.month == nowCmps.month) &&
- (selfCmps.day == nowCmps.day);
- }
- /**
- * 是否为昨天
- */
- - (BOOL)rq_isYesterday
- {
- // 2014-05-01
- NSDate *nowDate = [[NSDate date] rq_dateWithYMD];
-
- // 2014-04-30
- NSDate *selfDate = [self rq_dateWithYMD];
-
- // 获得nowDate和selfDate的差距
- NSCalendar *calendar = [NSCalendar currentCalendar];
- NSDateComponents *cmps = [calendar components:NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];
- return cmps.day == 1;
- }
- - (NSDate *)rq_dateWithYMD
- {
- NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
- fmt.dateFormat = @"yyyy-MM-dd";
- NSString *selfStr = [fmt stringFromDate:self];
- return [fmt dateFromString:selfStr];
- }
- /**
- * 是否为今年
- */
- - (BOOL)rq_isThisYear
- {
- NSCalendar *calendar = [NSCalendar currentCalendar];
- int unit = NSCalendarUnitYear;
-
- // 1.获得当前时间的年月日
- NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
-
- // 2.获得self的年月日
- NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
-
- return nowCmps.year == selfCmps.year;
- }
- // This hard codes the assumption that a week is 7 days
- - (BOOL) rq_isSameWeekWithAnotherDate: (NSDate *)anotherDate
- {
- NSDateComponents *components1 = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
-
- NSDateComponents *components2 = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:anotherDate];
-
- // Must be same week. 12/31 and 1/1 will both be week "1" if they are in the same week
- if (components1.weekOfMonth != components2.weekOfMonth) return NO;
-
- // Must have a time interval under 1 week. Thanks @aclark
- return (fabs([self timeIntervalSinceDate:anotherDate]) < RQ_D_WEEK);
- }
- /**
- * 星期几
- */
- - (NSString *)rq_weekDay
- {
- NSArray *weekDays = @[@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"];
-
- NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
-
- // NSLog(@"NSCalendarUnitWeekday-- components.weekday = %zd -- components.weekdayOrdinal = %zd -- components.weekOfYear = %zd -- components.weekOfMonth = %zd",components.weekday , components.weekdayOrdinal , components.weekOfYear , components.weekOfMonth);
-
- return weekDays[components.weekday - 1];
- }
- - (BOOL) rq_isThisWeek
- {
- return [self rq_isSameWeekWithAnotherDate:[NSDate date]];
- }
- /**
- * 通过一个时间 固定的时间字符串 "2016-8-10 14:43:45" 返回时间
- *
- * @param timestamp 固定的时间字符串 "2016-8-10 14:43:45"
- *
- * @return 时间
- */
- + (instancetype)rq_dateWithTimestamp:(NSString *)timestamp
- {
- return [[NSDateFormatter rq_defaultDateFormatter] dateFromString:timestamp];
- }
- /**
- * 返回固定的 当前时间 2016-8-10 14:43:45
- */
- + (NSString *)rq_currentTimestamp
- {
- return [[NSDateFormatter rq_defaultDateFormatter] stringFromDate:[NSDate date]];
- }
- /**
- * 格式化日期描述
- */
- - (NSString *)rq_formattedDateDescription
- {
- NSDateFormatter *dateFormatter = nil;
- NSString *result = nil;
-
- if ([self rq_isThisYear])
- {
- // 今年
- if ([self rq_isToday]) {
- // 22:22
- dateFormatter = [NSDateFormatter rq_dateFormatterWithFormat:@"HH:mm"];
- }else if ([self rq_isYesterday]){
- // 昨天 22:22
- dateFormatter = [NSDateFormatter rq_dateFormatterWithFormat:@"昨天 HH:mm"];
- }else if ([self rq_isThisWeek]){
- // 星期二 22:22
- dateFormatter = [NSDateFormatter rq_dateFormatterWithFormat:[NSString stringWithFormat:@"%@ HH:mm" , [self rq_weekDay]]];
- }else{
- // 2016年08月18日 22:22
- dateFormatter = [NSDateFormatter rq_dateFormatterWithFormat:@"yyyy年MM月dd日 HH:mm"];
- }
-
- }else{
- // 非今年
- dateFormatter = [NSDateFormatter rq_dateFormatterWithFormat:@"yyyy年MM月dd日"];
- }
-
- result = [dateFormatter stringFromDate:self];
-
- return result;
- }
- /** 与当前时间的差距 */
- - (NSDateComponents *)rq_deltaWithNow
- {
- NSCalendar *calendar = [NSCalendar currentCalendar];
- int unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
- return [calendar components:unit fromDate:self toDate:[NSDate date] options:0];
- }
- + (BOOL)isBetweenFromHour:(NSInteger)fromHour toHour:(NSInteger)toHour {
- NSDate *dateFrom = [NSDate getCustomDateWithHour:fromHour];
- NSDate *dateTo = [NSDate getCustomDateWithHour:toHour];
- NSDate *currentDate = [NSDate date];
- if ([currentDate compare:dateFrom]==NSOrderedDescending && [currentDate compare:dateTo]==NSOrderedAscending) {
- NSLog(@"该时间在 %ld:00-%ld:00 之间!", (long)fromHour, (long)toHour);
- return YES;
- }
- return NO;
- }
- + (NSDate *)getCustomDateWithHour:(NSInteger)hour {
- //获取当前时间
- NSDate *currentDate = [NSDate date];
- NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
- NSDateComponents *currentComps = [[NSDateComponents alloc] init];
- NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
- currentComps = [currentCalendar components:unitFlags fromDate:currentDate];
- //设置当天的某个点
- NSDateComponents *resultComps = [[NSDateComponents alloc] init];
- [resultComps setYear:[currentComps year]];
- [resultComps setMonth:[currentComps month]];
- [resultComps setDay:[currentComps day]];
- [resultComps setHour:hour];
-
- NSCalendar *resultCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
- return [resultCalendar dateFromComponents:resultComps];
- }
- //////////// MVC&MVVM的商品的发布时间的描述 ////////////
- - (NSString *)rq_string_yyyy_MM_dd {
- return [self rq_string_yyyy_MM_dd:[NSDate date]];
- }
- - (NSString *)rq_string_yyyy_MM_dd:(NSDate *)toDate {
- // 设置日期格式(声明字符串里面每个数字和单词的含义)
- // E:星期几
- // M:月份
- // d:几号(这个月的第几天)
- // H:24小时制的小时
- // m:分钟
- // s:秒
- // y:年
- // fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
-
- NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
- // 真机调试,转换这种欧美时间,需要设置locale
- fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
- // fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
- // fmt.locale = [NSLocale currentLocale];
-
- // 由于小闲肉使用的是北京时间,需要减掉8小时时差(转为欧美时间体系)
- NSDate *fromDate = [NSDate dateWithTimeIntervalSince1970:(self.timeIntervalSince1970 - 8*60*60)];
-
- // 日历对象(方便比较两个日期之间的差距)
- NSCalendar *calendar = [NSCalendar currentCalendar];
- // NSCalendarUnit枚举代表想获得哪些差值
- NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
- // 计算两个日期之间的差值
- NSDateComponents *cmps = [calendar components:unit fromDate:fromDate toDate:toDate options:0];
-
- if (cmps.year == 0) { // 今年
- if (cmps.year == 0 && cmps.month == 0 && cmps.day == 1) { // 昨天
- // fmt.dateFormat = @"昨天 HH:mm";
- fmt.dateFormat = @"MM-dd HH:mm";
- return [fmt stringFromDate:fromDate];
- } else if (cmps.year == 0 && cmps.month == 0 && cmps.day == 0) { // 今天
- if (cmps.hour >= 1) {
- return [NSString stringWithFormat:@"%d小时前", (int)cmps.hour];
- } else if (cmps.minute >= 1) {
- return [NSString stringWithFormat:@"%d分钟前", (int)cmps.minute];
- } else {
- return @"刚刚";
- }
- } else { // 今年的其他日子
- fmt.dateFormat = @"MM-dd HH:mm";
- return [fmt stringFromDate:fromDate];
- }
- } else { // 非今年
- fmt.dateFormat = @"yyyy-MM-dd HH:mm";
- return [fmt stringFromDate:fromDate];
- }
- }
- //////////// MVC&MVVM的商品的发布时间的描述 ////////////
- @end
|