NSDate 得到各种想要日期
2019-11-07 本文已影响0人
简化
这篇文件将会得到以下日期:
1.当前时间
2.今日凌晨
3.这个星期 这个星期星期一的日期
4.上个星期 星期一的日期
(约定每周一 为 一周 的开始)
代码如下:
获取今日凌晨时间:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSUIntegerMax fromDate:[NSDate date]];
components.hour = 0;
components.minute = 0;
components.second = 0;
//获取趋势时间
NSTimeInterval ts = (double)(int)[[calendar dateFromComponents:components] timeIntervalSince1970];
NSDate * date = [NSDate dateWithTimeIntervalSince1970:ts];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSLog(@"时区是%@",zone);
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate *localeZeroDate = [date dateByAddingTimeInterval: interval];
NSLog(@"本地凌晨时间是%@",localeZeroDate);
//下边的语句输出应该是 Thu Nov 7 14:35:08 2019 这样的格式
NSDate* temoDate = [[NSDate alloc] init];
NSLog(@"%@",temoDate);
第一个语句[NSDate date]
得到的时间是2019-11-07 06:38:56 +0000
也就是 2019-11-07 06:38:56 UTC
。 当我们设置 hour minute second
为0
的时候得到的就是UTC 的凌晨时间。所以为了得到我们的凌晨时间,就需要加上时区的偏移量。利用secondsFromGMTForDate
方法获取时区的偏移量,再增加到GMT的凌晨时间上来,就是本地的凌晨时间显示了。
NSTimeInterval ts = (double)(int)[[calendar dateFromComponents:components] timeIntervalSince1970];// 从格林威治时间 到 今天 的时间差
NSDate * date = [NSDate dateWithTimeIntervalSince1970:ts];//date = 今天格林威治凌晨 = 格林威治时间 + 时间差 (这里虽然看起来重复,但是保存了格林威治时间这个时间点下来)
NSTimeZone *zone = [NSTimeZone systemTimeZone];//
NSLog(@"时区是%@",zone);
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate *localZeroDate = [date dateByAddingTimeInterval: interval];
获取当前时间:
NSTimeInterval ts = (double)(int)[[calendar dateFromComponents:components] timeIntervalSince1970];// 从格林威治时间 到 今天 的时间差
NSDate * date = [NSDate dateWithTimeIntervalSince1970:ts];//date = 今天格林威治凌晨 = 格林威治时间 + 时间差 (这里虽然看起来重复,但是保存了格林威治时间这个时间点下来)
NSTimeZone *zone = [NSTimeZone systemTimeZone];//
NSLog(@"时区是%@",zone);
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate *localDate = [[NSDate date] dateByAddingTimeInterval: interval];//UTC 时间加上偏移量
components 的属性:

有了上边的凌晨 和 当前设置其他就简单了。
剩余时间
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate:[[NSDate alloc] init]];
[components setHour:-[components hour]];
[components setMinute:-[components minute]];
[components setSecond:-[components second]];
//NSDate *today = [cal dateByAddingComponents:components toDate:[[NSDate alloc] init] options:NSCalendarMatchStrictly]; //This variable should now be pointing at a date object that is the start of today (midnight);
NSDate *today = [cal dateByAddingComponents:components toDate:[[NSDate alloc] init] options:0];
self.today = today;
[components setHour:-24];
[components setMinute:0];
[components setSecond:0];
NSDate *yesterday = [cal dateByAddingComponents:components toDate: today options:0];
self.yesterday = yesterday;
components = [cal components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[[NSDate alloc] init]];
self.weekDays = [components weekday];//这里是记录这周过了几天,排除今天
//NSLog(@"%@",components);
[components setDay:([components day] - ([components weekday] - 2))];// 星期一为数字 2, 所以今天是周一 那么加上自己的weekday + offset -> offset = -2
//NSLog(@"%@",components);
NSDate *thisWeek = [cal dateFromComponents:components];
self.thisWeek = thisWeek;
[components setDay:([components day] - 7)];
NSDate *lastWeek = [cal dateFromComponents:components];
self.lastWeek = lastWeek;
[components setDay:([components day] - ([components day] -1))];
NSDate *thisMonth = [cal dateFromComponents:components];
self.thisMonth = thisMonth;
[components setMonth:([components month] - 1)];
NSDate *lastMonth = [cal dateFromComponents:components];
self.lastMonth = lastMonth;
NSString * dateDisplay = [dateFormatter stringFromDate:today];
NSLog(@"today = %@",dateDisplay);
dateDisplay = [dateFormatter stringFromDate:yesterday];
NSLog(@"yesterday = %@",dateDisplay);
dateDisplay = [dateFormatter stringFromDate:thisWeek];
NSLog(@"thisWeek = %@",dateDisplay);
dateDisplay = [dateFormatter stringFromDate:lastWeek];
NSLog(@"lastWeek = %@",dateDisplay);
dateDisplay = [dateFormatter stringFromDate:thisMonth];
NSLog(@"thisMonth = %@",dateDisplay);
dateDisplay = [dateFormatter stringFromDate:lastMonth];
这里注意下 NSDate的输出格式可能有 2019/11/7, 12:00:00 AM GMT+8 或者 2019/11/7, 00:00:00 GMT+8 这两个是等价的,但是不同的iOS版本,现实的结果不一样。应该不会有什么影响。 里边的weekday
该参数是 234561 也就是 1234567(星期)
这个地方会争议 [components setDay:([components day] - ([components weekday] - 2))];
因为我用来统计今天之前的一周之内的数据,所以234567可以这样写。2 - 2 = 0刚好是今日,也就是这周的开始。所以这周,该怎么使用需要看实际的需求时怎样的了。