扩展添加了获取年,月,日,时,分,秒
获取日期和时间的字符串,
获取带有偏移量的日期,前几天,前几月等
获取当前时间是在昨天还是前天等等
//
// NSDate+Helpers.m
// RProject
//
// Created by ray on 2017/8/19.
// Copyright © 2017年 ray. All rights reserved.
//
#import "NSDate+RHelpers.h"
@implementation NSDate (RHelpers)
/**
获取当前年月日:19700101
@return 19700101
*/
-(NSString *)getFormatYMD{
return [NSString stringWithFormat:@"%lu%02lu%02lu", (unsigned long)[self getYear],(unsigned long)[self getMonth],(unsigned long)[self getDay]];
}
#pragma mark 获取日期 时间 👇
/**
获取日
@return 19
*/
-(NSUInteger)getDay{
return [[self getDateComponents:NSCalendarUnitDay fromDate:self] day];
}
/**
获取月
@return 12
*/
-(NSUInteger)getMonth{
return [[self getDateComponents:NSCalendarUnitMonth fromDate:self] month];
}
/**
获取年
@return 2017
*/
-(NSUInteger)getYear{
return [[self getDateComponents:NSCalendarUnitYear fromDate:self] year];
}
/**
获取 时
@return int 几点
*/
-(int)getHour{
return [self getHour:self];
}
/**
获取 分
@return int 几分
*/
-(int)getMinute{
return [self getMinute:self];
}
/**
获取 时
@return int 几点
*/
-(int)getHour:(NSDate *)date{
return (int)[[self getDateComponents:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate:date] hour];
}
/**
获取 分
@return int 几分
*/
-(int)getMinute:(NSDate *)date{
return (int)[[self getDateComponents:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate:date] minute];
}
/**
获取NSDateComponents对象
@param unitFlags NSDateComponents的枚举类型 年/月/日?
@return NSDateComponents对象
*/
-(NSDateComponents *)getDateComponents:(NSCalendarUnit)unitFlags fromDate:(NSDate *)date{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:unitFlags fromDate:date];
return comp;
}
#pragma mark 获取时间字符串👇
/**
获取时间字符串
@param date NSDate对象
@param format 时间格式
@return 返回时间字符串
*/
-(NSString *)stringFromDate:(NSDate *)date withFormat:(NSString *)format{
return [date dateStringWithFormat:format];
}
/**
获取时间字符串
@param date NSDate对象
@return 返回时间字符串
*/
-(NSString *)stringFromDate:(NSDate *)date{
return [date string];
}
/**
获取时间字符串
@param format 时间戳格式1991-01-01 12:12:12
@return 返回时间的字符串
*/
-(NSString *)dateStringWithFormat:(NSString *)format{
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc]init];
[outputFormatter setDateFormat:format];
NSString *timestamp_str = [outputFormatter stringFromDate:self];
return timestamp_str;
}
/**
获取时间字符串
@return 1970-09-01 12:12:12
*/
-(NSString *)string{
return [self dateStringWithFormat:[NSDate datetampFormatString]];
}
/**
获取时间字符串
@param dateStyle 日期格式
@param timeStyle 时间格式
@return 时间字符串
*/
-(NSString *)stringWithDateStyle:(NSDateFormatterStyle)dateStyle timeStyle:(NSDateFormatterStyle)timeStyle{
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc]init];
[outputFormatter setDateStyle:dateStyle];
[outputFormatter setTimeStyle:timeStyle];
NSString *outputString = [outputFormatter stringFromDate:self];
return outputString;
}
#pragma mark NSDate offset 获取带有偏移量的日期👇
/**
获取今天+偏移量 的日期
@param day 偏移几天
@return 偏移后的日期
*/
-(NSDate *)dateAfterDay:(int)day{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateCompToAdd = [[NSDateComponents alloc]init];
[dateCompToAdd setDay:day];
NSDate *dateAfterDay = [calendar dateByAddingComponents:dateCompToAdd toDate:self options:0];
return dateAfterDay;
}
/**
获取今天+偏移量 的日期
@param month 偏移几月
@return 偏移后的日期
*/
-(NSDate *)dateAfterMonth:(int)month{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateCompToAdd = [[NSDateComponents alloc]init];
[dateCompToAdd setMonth:month];
NSDate *dateAfterDay = [calendar dateByAddingComponents:dateCompToAdd toDate:self options:0];
return dateAfterDay;
}
/**
获取当前日期距离今天已经过去了几天
@return 过去了几天
*/
-(NSUInteger)daysAgo{
NSCalendar *calender = [NSCalendar currentCalendar];
NSDateComponents *comp = [calender components:NSCalendarUnitDay fromDate:self toDate:[NSDate date] options:0];
return [comp day];
}
/**
获取日期距离今天午夜过去几天
@return 过去几天
*/
-(NSUInteger)daysAgoAgainstMidnight{
NSDateFormatter *mdf = [[NSDateFormatter alloc]init];
[mdf setDateFormat:@"yyyy-MM-dd"];
NSDate *midnight = [mdf dateFromString:[mdf stringFromDate:self]];
return (int)[midnight timeIntervalSinceNow] / (60*60*24) *-1;
}
/**
获取日期距离今天过去几天
@param flag 是否是午夜之后
@return 过去几天之后的字符串
*/
-(NSString *)stringDaysAgoAgainstMidnight:(BOOL)flag{
NSUInteger daysAgo = (flag) ? [self daysAgoAgainstMidnight] : [self daysAgo];
NSString *text = nil;
switch (daysAgo) {
case 0:
text = @"今天";
break;
case 1:
text = @"昨天";
break;
case 2:
text = @"前天";
break;
default:
text = [NSString stringWithFormat:@"%lu天前",(unsigned long)daysAgo];
break;
}
return text;
}
/**
获取日期距离今天过去几天
@return 过去几天之后的字符串
*/
-(NSString *)stringDaysAgo{
return [self stringDaysAgoAgainstMidnight:YES];
}
/**
获取当前日期是这周的第几天
@return 返回第几天
*/
-(NSUInteger)weekday{
NSCalendar *calender = [NSCalendar currentCalendar];
NSDateComponents *comp = [calender components:NSCalendarUnitWeekday fromDate:self];
return [comp weekday];
}
/**
返回当前天的年月日
@return 返回日期
*/
-(NSDate *)beginningOfDay{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:self];
return [calendar dateFromComponents:comp];
}
/**
返回当前日期的所在月份的第一天日期
@return 该月第一天
*/
-(NSDate *)beginningOfMonth{
return [self dateAfterDay:-(int)[self getDay] + (int)1];
}
/**
获取当月最后一天
@return NSDate month
*/
-(NSDate *)endOfMonth{
return [[[self beginningOfMonth] dateAfterMonth:1] dateAfterDay:-1];
}
/**
返回当前周的周末日期
@return NSDate 当周周末 日期
*/
-(NSDate *)endOfWeek{
NSCalendar *calendar = [NSCalendar currentCalendar];
// 获取当前日期是周几
NSDateComponents *weekdayComp = [calendar components:NSCalendarUnitWeekday fromDate:self];
NSDateComponents *compToAdd = [[NSDateComponents alloc]init];
// 获取当周的周末 7-当前星期
[compToAdd setDay:(7 - [weekdayComp weekday])];
NSDate *endOfWeek = [calendar dateByAddingComponents:compToAdd toDate:self options:0];
compToAdd = nil;
return endOfWeek;
}
/**
获取当前日期是该年的第几周
@return int 几周
*/
-(int)getWeekOfYear{
int i;
int year = (int)[self getYear];
NSDate *date = [self endOfWeek];
for (i = 1; [[date dateAfterDay:-7*i] getYear] == year; i++) {
}
return i;
}
#pragma mark 获取时间戳格式字符串 👇
/**
获取日月年格式字符串
@return yyyy-MM-dd
*/
+(NSString *)dateFormatString{
return @"yyyy-MM-dd";
}
/**
获取时分秒格式字符串
@return HH:mm:ss
*/
+(NSString *)timeFormatString{
return @"HH:mm:ss";
}
/**
获取时间戳格式字符串
@return yyyy-MM-dd HH:mm:ss
*/
+(NSString *)timestampFormatString{
return @"yyyy-MM-dd HH:mm:ss";
}
/**
获取时间戳格式字符串
@return yyyy-MM-dd HH:mm:ss
*/
+(NSString *)datetampFormatString{
return [NSDate timestampFormatString];
}
#pragma mark 将时间字符串转位NSDate👇
/**
时间字符串转位NSDate
@param string 时间字符串
@return NSDate
*/
+(NSDate *)dateFromString:(NSString *)string{
return [NSDate dateFromString:string withFormat:[NSDate datetampFormatString]];
}
/**
时间字符串转为NSDate
@param string 时间字符串
@param format 日期格式
@return NSDate
*/
+(NSDate *)dateFromString:(NSString *)string withFormat:(NSString *)format{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:format];
return [formatter dateFromString:string];
}
#pragma mark 时间转换 将指定的时间转换为当前上/下午00:00|星期X|08-10|1999-09-01👇
/**
获取从某日期到当前日期的显示字符串
@param date NSDate对象
@param prefixed ?
@return 返回显示字符串
*/
+(NSString *)stringForDisplayFromDate:(NSString *)date prefixed:(BOOL)prefixed{
NSDate *today = [[NSDate alloc]init];
NSDate *dateTmp = [NSDate dateFromString:date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *offsetComponents = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:today];
NSDate *midnight = [calendar dateFromComponents:offsetComponents];
NSDateFormatter *displayFormatter = [[NSDateFormatter alloc]init];
NSString *displayString = nil;
if ([dateTmp compare:midnight] == NSOrderedDescending) {
if (prefixed) {
[displayFormatter setDateFormat:@"'at'ah:mm"];
}else{
[displayFormatter setDateFormat:@"ah:mm"];
}
}else{
// 判断是否是最近七天
NSDateComponents *compToSubstract = [[NSDateComponents alloc]init];
[compToSubstract setDay:-7];
NSDate *lastWeek = [calendar dateByAddingComponents:compToSubstract toDate:today options:0];
if ([dateTmp compare:lastWeek] == NSOrderedDescending) {
[displayFormatter setDateFormat:@"EEEE"];
} else {
// 判断年分
NSInteger thisYear = [offsetComponents year];
NSDateComponents *dateComp = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth) fromDate:dateTmp];
NSInteger thatYear = [dateComp year];
if (thatYear >= thisYear) {
[displayFormatter setDateFormat:@"MM-dd"];
} else {
[displayFormatter setDateFormat:@"yyyy-MM-dd"];
}
}
if (prefixed) {
NSString *dateFormat = [displayFormatter dateFormat];
NSString *prefix = @"on";
[displayFormatter setDateFormat:[prefix stringByAppendingString:dateFormat]];
}
}
displayString = [displayFormatter stringFromDate:dateTmp];
return displayString;
}
/**
获取从某日期到当前日期的显示字符串
@param date NSDate对象
@return 返回时间字符串
*/
+(NSString *)stringForDisplayFromDate:(NSString *)date{
return [NSDate stringForDisplayFromDate:date prefixed:NO];
}
@end
网友评论