判斷兩個日期是否在同一周 寫在NSDate的category里面
- (BOOL)isSameDateWithDate:(NSDate *)date
{
//日期間隔大于七天之間返回NO
if (fabs([self timeIntervalSinceDate:date]) >= 7 * 24 *3600)
{
return NO;
}
NSCalendar *calender = [NSCalendar currentCalendar];
calender.firstWeekday = 2;//設置每周第一天從周一開始
//計算兩個日期分別為這年第幾周
NSUInteger countSelf = [calender ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitYear forDate:self];
NSUInteger countDate = [calender ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitYear forDate:date];
//相等就在同一周霜瘪,不相等就不在同一周
return countSelf == countDate;
}
3CCA07DC-1187-4777-B6EF-0E8C99E1B50A.png
只要是通過date 方法創(chuàng)建的時間對象, 對象中就保存了當前的時間
//當前時間 2016-09-03 14:12:23 + 0000 [時區(qū) 0 時區(qū)] 我們是東八區(qū)
- NSDate *now = [NSDate date];
//當前時間 的基礎上 加上 10秒 - NSDate *date = [ now dateByAddingTimeInterval: 10];
2D555F12-C10A-44DF-BF8D-0C8C5AC55A92.png
419759D1-C3CA-4B28-98DD-E452BD1C1BEA.png