1//当前时间创建NSDate
NSDate*myDate = [NSDatedate];
NSLog(@"myDate
= %@",myDate);
2//从现在开始的24小时
NSTimeIntervalsecondsPerDay =24*60*60;
NSDate*tomorrow = [NSDatedateWithTimeIntervalSinceNow:secondsPerDay];
NSLog(@"myDate
= %@",tomorrow);
3//根据已有日期创建日期
NSTimeIntervalsecondsPerDay1 =24*60*60;
NSDate*now = [NSDatedate];
NSDate*yesterDay = [nowaddTimeInterval:-secondsPerDay1];
NSLog(@"yesterDay
= %@",yesterDay);
4//比较日期
BOOLsameDate = [nowisEqualToDate:yesterDay];
NSLog(@"sameDate
= %lu",sameDate);
4.1//获取较早的日期
NSDate*earlierDate = [yesterDayearlierDate:now];
NSLog(@"earlierDate
= %@",earlierDate);
4.2//较晚的日期
NSDate*laterDate = [yesterDaylaterDate:now];
NSLog(@"laterDate
= %@",laterDate);
//两个日期之间相隔多少秒
NSTimeIntervalsecondsBetweenDates= [yesterDaytimeIntervalSinceDate:now];
NSLog(@"secondsBetweenDates=
%lf",secondsBetweenDates);
//通过NSCALENDAR类来创建日期
NSDateComponents*comp
= [[NSDateComponentsalloc]init];
[compsetMonth:06];
[compsetDay:01];
[compsetYear:2001];
NSCalendar*myCal
= [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDate*myDate1 = [myCaldateFromComponents:comp];
NSLog(@"myDate1
= %@",myDate1);
//从已有日期获取日期
unsignedunits
=NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit;
NSDateComponents*comp1 = [myCalcomponents:unitsfromDate:now];
NSIntegermonth = [comp1month];
NSIntegeryear = [comp1year];
NSIntegerday = [comp1day];
//NSDateFormatter实现日期的输出
NSDateFormatter*formatter
= [[NSDateFormatteralloc]init];
[formattersetDateStyle:NSDateFormatterFullStyle];//直接输出的话是机器码
//或者是手动设置样式[formatter setDateFormat:@"yyyy-mm-dd"];
NSString*string = [formatterstringFromDate:now];
NSLog(@"string
= %@",string);
NSLog(@"formater
= %@",formatter);
//获取日期格式对象
- (NSDateFormatter*)dateFormatter {
if(dateFormatter==nil) {
dateFormatter= [[NSDateFormatteralloc]init];
[dateFormattersetDateStyle:NSDateFormatterMediumStyle];
[dateFormattersetTimeStyle:NSDateFormatterNoStyle];
}
returndateFormatter;
}