博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【语法】NSDate
阅读量:6871 次
发布时间:2019-06-26

本文共 2210 字,大约阅读时间需要 7 分钟。

NSDate

======================== ios 如何获得系统时间和日期===================

1. 获得系统时间

1       NSDate *senddate=[NSDate date];2         NSDateFormatter *dateformatter=[[[NSDateFormatter alloc] init] autorelease];3         [dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss-a"];4         NSString *locationString =[dateformatter stringFromDate: senddate];5         NSLog(@"当前时间=%@",locationString);

 

2.获得当前时间【好像是以0时区为标准】

     NSDate *myDate = [NSDate date];        NSLog(@"myDate = %@",myDate);

 

3.获得系统日期

    NSCalendar *cal = [NSCalendar currentCalendar];        NSUInteger unitFlags = NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit;        NSDateComponents * conponent = [cal components:unitFlags fromDate:senddate];        NSInteger year = [conponent year];        NSInteger month = [conponent month];        NSInteger day = [conponent day];        NSString *dateString = [NSString stringWithFormat:@"%4d年%2d月%2d日",(int)year,(int)month,(int)day];        NSLog(@"当前日期 = %@",dateString);

 

4.从字符串来获得NSDate

1        NSString *strYear = @"1988"; 2         NSString *strMonth = @"09"; 3         NSString *strDay = @"18"; 4         NSString *strHour = @"5"; 5         NSString *strMinutes = @"18"; 6         NSString *strSec = @"20"; 7          8         NSString *morelocationString = [NSString stringWithFormat:@"%@-%@-%@ %@:%@:%@" 9                                         ,strYear,strMonth,strDay,strHour,strMinutes,strSec];10         11         //根据时间字符串获得NSDate12         NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];13         [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // 这个格式必须跟33行一致14         NSDate *oldDate = [formatter dateFromString:morelocationString];15         NSLog(@"oldDate = %@",[dateformatter stringFromDate:oldDate]);

 

5. 第二天的当前时间

1         NSTimeInterval secondsPerDay = 24*60*60;2         NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];3         NSLog(@"myDate = %@",tomorrow);

 

6.前一天的当前时间

     NSTimeInterval secondsPerDay1 = 24*60*60;        NSDate *now = [NSDate date];        NSDate *yesterDay = [now addTimeInterval:-secondsPerDay1];        NSLog(@"yesterDay = %@",yesterDay);

 

转载于:https://www.cnblogs.com/madeininfi/p/3650357.html

你可能感兴趣的文章
《程序员之禅》一一10.10 淡泊宁静
查看>>
《MATLAB图像处理超级学习手册》一一2.1 矩阵的创建
查看>>
还在用密码登录 ECS?ECS 控制台更安全的 SSH 密钥对 来了
查看>>
哪种编程语言最流行?而这到底又意味着什么
查看>>
为什么企业不对 GPL 侵权采取法律行动
查看>>
《Spark大数据分析实战》——第1章Spark简介
查看>>
5.5确认范围
查看>>
Chris Grainger:我们如何才能更好地编程?
查看>>
以黑客教主之名,TK 发现 Windows 史上最大漏洞
查看>>
《IPv6精髓(第2版)》——导读
查看>>
《Windows Server 2012 Hyper-V虚拟化管理实践》一1.2 Hyper-V安装前后的变化
查看>>
Proxmox VE 4.4 发布,新 Ceph 仪表盘上线
查看>>
《CCNP TSHOOT(642-832)学习指南》一1.2 维护进程及维护流程
查看>>
华为宣布开源流处理平台查询语言 StreamCQL
查看>>
2016 年 6 月 RedMonk 编程语言排行榜
查看>>
《Adobe Photoshop CC经典教程(彩色版)》—第1课1.4节在Photoshop中还原操作
查看>>
HttpClient使用详解
查看>>
增强现实?先不要指望那些眼镜了
查看>>
《iOS 6核心开发手册(第4版)》——1.10节秘诀:使用多触摸交互
查看>>
《云数据管理:挑战与机遇》一第1章
查看>>