刚刚做IOS开发第一个需求的时候,需要判断是否为同一天,最近需求需要判断两个日期是否为同一周,在网上没有找到合适的方法,自己写了一个,目前用着还行。
相关方法链接(待补充)
NSDate NSDateFormatter NSDateComponents NSDateComponentFormatter NSCalendar
1、NSDate的常用方法
1.1初始化方法
// 创建方法
NSDate *date = [NSDate date];
NSLog(@"date %@", date);
2018-09-26 16:10:23.258016+0800[6440:2239584] date时间 = 2018-09-26 08:10:23 +0000
其他初始化方法
//初始化当前时间,返回零时区时间
NSDate *date = [NSDate date];
//以当前时间为准,正数超前指定秒数,负数延后指定秒数
NSDate *laterDate = [NSDate dateWithTimeIntervalSinceNow:60];
//以2001-01-01 00:00:00 +0000为基准,正数超前指定秒数,负数延后指定秒数
NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:60];
//以1970-01-01 00:00:00 +0000为基准,正数超前指定秒数,负数延后指定秒数
NSDate *newDate1 = [NSDate dateWithTimeIntervalSince1970:60];
//实例方法,以指定时间为基准,正数超前指定秒数,负数延后指定秒数
NSDate *newDate2 = [date dateByAddingTimeInterval:60];
//很久以后的某一天
NSDate *newDate3 = [NSDate distantFuture];
//很久以前的某一天
NSDate *newDate4 = [NSDate distantPast];
时间比较方法
//比较两个时间是否相等
- (BOOL)isEqualToDate:(NSDate *)otherDate;
//两个时间比较,返回较早时间
- (NSDate *)earlierDate:(NSDate *)anotherDate;
//两个时间比较,返回较晚时间
- (NSDate *)laterDate:(NSDate *)anotherDate;
//两个时间比较,返回枚举类型
- (NSComparisonResult)compare:(NSDate *)other;
计算时间间隔
//返回实例时间与refDate时间间隔秒数
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)refDate;
//返回实例时间与当前时间间隔秒数
- (NSTimeInterval)timeIntervalSinceNow;
//返回实例时间的时间戳
- (NSTimeInterval)timeIntervalSince1970;
//返回实例时间和2001-01-01 00:00:00 +0000的间隔秒数
- (NSTimeInterval)timeIntervalSinceReferenceDate;
//返回当前时间和2001-01-01 00:00:00 +0000的间隔秒数
+ (NSTimeInterval)timeIntervalSinceReferenceDate;
1.2将时间转化为字符串
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSString *dateStr = [formatter stringFromDate:date];
NSLog(@"date时间 = %@",dateStr);
2018-09-26 16:10:34.223851+0800[6440:2239584] date时间 = 2018-09-26 16:10:23 +0800
1.3使用字符串初始化时间
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate *newDate = [formatter dateFromString:@"2018-09-26 15:06:24 +0800"];
NSLog(@"newDate = %@", newDate);
2018-09-26 16:15:23.072026+0800 [6448:2241325] newDate = 2018-09-26 07:06:24 +0000
NSDateFormatter的指定格式是:@"yyyy-MM-dd HH:mm:ss Z",这里面的Z指的是时区。要转化的字符串时间格式必须和这个格式匹配,上面给定的字符串时间是:"2018-09-26 15:06:24 +0800",是一个东八区时间,转化为NSDate后是零区时间''2018-09-26 07:06:24 +0000'',字面显示上少了8小时,其实时间一样。
-
NSDate转当前时区的NSDate时间
因为[NSDate date]得出的时间是零时区时间,当我们要获取当前所在时区的NSDate时间时,通常会用以下方法:
NSDate *date = [NSDate date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];//当前时区
NSInteger interval = [zone secondsFromGMTForDate:date];//当前时区和零时区的差值
NSDate *localDate = [date dateByAddingTimeInterval:interval];//加上差值
NSLog(@"localDate = %@",localDate);
2018-09-26 16:19:30.761683+0800 [6456:2242925] localDate = 2018-09-26 16:19:26 +0000
注:使用日期说明符来设置日期格式
EEEE:“星期”的全名(比如Monday)。如需缩写,指定1-3个字符(如E,EE,EEE代表Mon)。 MMMM:“月份”的全名(比如October)。如需缩写,指定1-3个字符(如M,MM,MMM代表Oct)。 dd:某月的第几天(例如,09或15) yyyy:四位字符串表示“年”(例如2015) HH:两位字符串表示“小时”(例如08或19) mm:两位字符串表示“分钟”(例如05或54) ss:两位字符串表示“秒” zzz:三位字符串表示“时区”(例如GMT)。缩写Z GGG:公元前BC或公元后AD
2、常见的两个日期判断方法
2.1判断两个日期是否为同一天
//判断是否为同一天
- (BOOL)isSameDay:(NSDate *)date1 date2:(NSDate *)date2
{
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlag = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *comp1 = [calendar components:unitFlag fromDate:date1];
NSDateComponents *comp2 = [calendar components:unitFlag fromDate:date2];
return (([comp1 day] == [comp2 day]) && ([comp1 month] == [comp2 month]) && ([comp1 year] == [comp2 year]));
}
2.2判断两个日期是否为同一周
NSDate使用weekday获得当天日期在本周的第几天。返回结果从周日开始计时,从周日到周一分别是[0 ... 6]。
//判断是否为同一周
- (BOOL)isSameWeeks:(NSDate *)date1 date2:(NSDate *)date2
{
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlag = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekday | NSCalendarUnitWeekOfYear |NSCalendarUnitWeekday;
NSDateComponents *comp1 = [calendar components:unitFlag fromDate:date1];
NSDateComponents *comp2 = [calendar components:unitFlag fromDate:date2];
if([comp1 weekday] == 1){
date1 = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:date1];
comp1 = [calendar components:unitFlag fromDate:date1];
}
if ([comp2 weekday] == 1) {
date2 = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:date2];
comp2 = [calendar components:unitFlag fromDate:date2];
}
return (([comp1 weekOfYear] == [comp2 weekOfYear]) && ([comp1 month] == [comp2 month]) && ([comp1 year] == [comp2 year]));
}
文章参考:https://blog.csdn.net/sqc3375177/article/details/53608867