一:通过时间戳除以每天的秒数,得到当前的天数,主要用来比较天数,用来制作不带时分秒的Date。
NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
int daySeconds = 24 * 60 * 60;
NSInteger theDays = interval / daySeconds;
//theDays: 16658
NSDate *theDate = [NSDate dateWithTimeIntervalSince1970:allDays * daySeconds];
//theDate: 2015-08-11 00:00:00 +0000
二:可以获得当前日期的几乎所有日期相关
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"'公元前/后:'G '年份:'u'='yyyy'='yy '季度:'q'='qqq'='qqqq '月份:'M'='MMM'='MMMM '今天是今年第几周:'w '今天是本月第几周:'W '今天是今年第几天:'D '今天是本月第几天:'d '星期:'c'='ccc'='cccc '上午/下午:'a '小时:'h'='H '分钟:'m '秒:'s '毫秒:'SSS '这一天已过多少毫秒:'A '时区名称:'zzzz'='vvvv '时区编号:'Z "];
NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]);
三:获得当前日期的年,月,日,(时,分,秒)
NSDate *currentDate = [NSDate date];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentDate];
NSInteger m = [components month]; // month
NSInteger d = [components day]; // day
NSInteger y = [components year]; // year