NSDate 的一些操作(比较、创建、在现有date加减一定时间等)
来源:互联网 发布:辐射4 mac 下载 编辑:程序博客网 时间:2024/06/14 18:45
创建当前时间
- NSDate *date = [NSDate date];
从现在开始的24小时
NSTimeInterval a_day = 24*60*60;
NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:a_day];
根据已有日期创建日期
- NSTimeInterval a_day = 24*60*60;
- NSDate *date = [NSDate date];
- NSDate *tomorrow = [date addTimeInterval: date];
日期比较
- BOOL isEqual = [date isEqualToDate:tomorrow];
- f(isEqual){
- NSLog(@"equal");
//获取较早的日期
- NSDate *earlier_date = [tomorrow earlierDate:date];
- NSLog(@"earlierDate = %@",earlierDate);
//较晚的日期
- NSDate *laterDate = [tomorrow laterDate:date];
- NSLog(@"laterDate = %@",laterDate);
//两个日期之间相隔多少秒
- NSTimeInterval secondsInterval= [date timeIntervalSinceDate:tomorrow];
- NSLog(@"secondsInterval= %lf",secondsInterval);
//通过NSCalendar类来创建日期
- NSDateComponents *comps = [[NSDateComponentsalloc]init];
- [comps setMonth:01];
- [comps setDay:31];
- [comps setYear:2013];
- NSCalendar *calendar = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];
- NSDate *date = [calendar dateFromComponents:comps];
//从已有日期获取日期
- unsigned units = NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit;
- NSDateComponents *_comps = [calendar components:units fromDate:date];
- NSInteger month = [_comps month];
- NSInteger year = [_comps year];
- NSInteger day = [_comps day];
- //NSDateFormatter实现日期的输出
- NSDateFormatter *formatter = [[NSDateFormatteralloc]init];
- [formatter setDateStyle:NSDateFormatterFullStyle];//直接输出的话是机器码
- //或者是手动设置样式[formatter setDateFormat:@"yyyy-mm-dd"];
- NSString *dateString = [formatter stringFromDate:date];
- NSLog(@"dateString = %@",dateString);
- NSLog(@"formater = %@",formatter);
//获取日期格式对象
- - (NSDateFormatter *)getDateFormatter {
- if (dateFormatter == nil) {
- dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
- [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
- }
- return dateFormatter;
- }
0 0
- NSDate 的一些操作(比较、创建、在现有date加减一定时间等)
- NSDate 的一些操作(比较、创建、在现有date加减一定时间等)
- NSDate 的一些操作(比较、创建、在现有date加减一定时间等)
- NSDate 的一些操作(比较、创建、在现有date加减一定时间等)
- NSDate 的一些操作(比较、创建、在现有date加减一定时间等)
- Date的时间加减
- 最好用的JS时间date操作类(时间格式化、加减日、月、年、操作)
- oracle数据库的一些时间加减操作
- 关于数据库的一些操作! 实用为主! 有关于时间加减!临时表等……
- Date与String互转、对时间的加减操作
- java对Date类型时间的直接加减操作
- NSDate时间加减
- NSDate 时间加减
- 时间格式、比较等(2):NSDate与NSString的互相转化
- NSDate 的一些操作
- Oracle中日期时间的操作比较和加减
- Oracle数据库中日期时间的操作比较和加减
- Oracle中日期时间的操作比较和加减
- Window Routing and Remote Access 服务(RRAS)无法启动
- 控件中显示系统当前时间?包括:年、月、日、时、分
- 自定义装饰类
- alt和title的区别和写法
- 7th_drv.c
- NSDate 的一些操作(比较、创建、在现有date加减一定时间等)
- LeetCode(90) Subsets II
- Android Please ensure that adb is correctly located at问题解决
- 使用Cookie记住表单用户名
- 实用的AndroidStudio快捷键
- hdu 5460 Poker(暴力)
- Hive安装与配置——深入浅出学Hive
- 多git账户操作
- xinetd: unrecognized service