NSDate 整理笔记
来源:互联网 发布:淘宝买家怎么改价格 编辑:程序博客网 时间:2024/05/17 21:05
1、 当前时间
NSDate *nowDate = [NSDate date];
NSLog(@"nowDate = %@",nowDate);
2、从现在开始的24小时
NSTimeInterval secondsPerDay = 24*60*60;
NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];
NSLog(@"tomorrow = %@",tomorrow);
3、根据已有日期创建日期
NSTimeInterval secondsPerDay1 = 24*60*60;
NSDate *now = [NSDate date];
NSDate *yesterDay = [now addTimeInterval:-secondsPerDay1];
NSLog(@"yesterDay = %@",yesterDay);
4、比较日期
BOOL sameDate = [now isEqualToDate:yesterDay];
NSLog(@"sameDate = %lu",sameDate);
4.1、获取较早的日期
NSDate *earlierDate = [yesterDay earlierDate:now];
NSLog(@"earlierDate = %@",earlierDate);
4.2、较晚的日期
NSDate *laterDate = [yesterDay laterDate:now];
NSLog(@"laterDate = %@",laterDate);
4.3、两个日期之间相隔多少秒
NSTimeInterval secondsBetweenDates= [yesterDay timeIntervalSinceDate:now];
NSLog(@"secondsBetweenDates= %lf",secondsBetweenDates);
4.4、通过NSCALENDAR类来创建日期
NSDateComponents *comp = [[NSDateComponents alloc]init];
[comp setMonth:06];
[comp setDay:01];
[comp setYear:2001];
NSCalendar *myCal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *myDate1 = [myCal dateFromComponents:comp];
NSLog(@"myDate1 = %@",myDate1);
4.5、从已有日期获取日期
unsigned units = NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit;
NSDateComponents *comp1 = [myCal components:units fromDate:now];
NSInteger month = [comp1 month];
NSInteger year = [comp1 year];
NSInteger day = [comp1 day];
4.6、NSDateFormatter实现日期的输出
NSDateFormatter *formatter = [[NSDateFormatteralloc]init];
[formatter setDateStyle:NSDateFormatterFullStyle];//直接输出的话是机器码
4.7、或者是手动设置样式[formatter setDateFormat:@"yyyy-mm-dd"];
NSString *string = [formatter stringFromDate:now];NSLog(@"string = %@",string);
NSLog(@"formater = %@",formatter);
4.8、获取日期格式对象
- (NSDateFormatter *)dateFormatter {if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
}
return dateFormatter;
}
参考:http://www.cocoachina.com/bbs/read.php?tid=140980&keyword=NSDate
- NSDate 整理笔记
- NSDate整理
- 日常NSDate使用整理
- 日常NSDate使用整理
- NSDate类方法整理
- NSDate 相关整理(OC)
- NSDate 时差问题 笔记
- OC笔记NSDate
- NSDate总结 (学习笔记)
- NSDate--自学习笔记
- NSDate的平时笔记
- 【Foundation学习笔记】时间NSDate
- NSDate
- NSDate
- NSDate
- NSDate
- NSDate
- NSDate
- NoSQL非关系数据库简介
- CSS中怎么让DIV居中
- 现有分布式技术(socket、.net remoting、asp.net webservice、WSE、ES)和wcf的比较及优势
- Android 常用的距离单位
- 关于线程的停止和挂起
- NSDate 整理笔记
- SQL语句中查找条目中包含某一字符串的方法
- jQuery实现文件下载( 隐藏表单实现ajax下载 )
- webkit内核几篇学习的文章
- Aix6.1安装WebSphere(版本: 7.0.0.19)
- linux getopt函数 分析命令行参数
- bash: /usr/bin/yum: /usr/bin/python: bad interpreter: No such file or direct
- 程序人生之我们的故事:十年如歌(1)
- aaaaaaaaaaaa