ios 如何获得系统时间和日期
2024-09-02 04:17:51
iphone 如何获得系统时间和日期
代码如下:
#import <time.h>
1。获得当前的系统时间和日期
- //获得系统时间
- NSDate * senddate=[NSDate date];
- NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
- [dateformatter setDateFormat:@"HH:mm"];
- NSString * locationString=[dateformatter stringFromDate:senddate];
- //[dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
- //NSString * morelocationString=[dateformatter stringFromDate:senddate];
- //获得系统日期
- 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 * nsDateString= [NSString stringWithFormat:@"%4d年%2d月%2d日",year,month,day];
- [dateformatter release];
2。从字符串来获得NSDate
- string strYear="1988";
- string strMonth="09";
- string strDay="18";
- string strHour="5";
- string strMinutes="18";
- string strSec="20";
- morelocationString=[NSString stringWithFormat:@"%s-%s-%s-%s-%s-%s",strYear.c_str(),strMonth.c_str(),
- strDay.c_str(),strHour.c_str(),strMinutes.c_str(),strSec.c_str()];
- //根据时间字符串获得NSDate
- NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
- [dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
- NSDate * oldDate=[dateformatter dateFromString:morelocationString];
通过上面的代码,获得了NSDate。
3。 从GMT时间,得到本地时间
- NSDate * oldDate=[dateformatter dateFromString:morelocationString];
- NSTimeInterval timeZoneOffset=[[NSTimeZone systemTimeZone] secondsFromGMT];
- NSDate * newDate=[oldDate dateByAddingTimeInterval:timeZoneOffset];
最新文章
- cordova plugin数据传递概要
- WPF:将HTML RGB颜色值转化为Color对象的两种方式
- Qt设置horizontal line 和vertical line 的颜色
- API断点大全
- Oracle系列之异常处理
- Android学习笔记(六)Fragment的生命周期
- centos6 Cacti部署文档
- 从零开始制作Minecraft启动器(C++开源)
- Cocos2d-x C++调用Android弹出提示框
- Java下获取可用CPU数
- 51 NOD 1238 最小公倍数之和 V3
- Problem - 1062 http://acm.hdu.edu.cn/showproblem.php?pid=1062
- lambda 和 iterable
- Win10系列:VC++ XML文件解析
- IEnumerator和IEnumerable详解
- 行为类模式(十一):访问者(Visitor)
- Java虚拟机(三):Java 类的加载机制
- phpStudy7——MySql数据库的增删查改
- jquery记忆笔记
- thinkphp5集成微信支付【公众号支付】快捷路径
热门文章
- 从一段字符串中去除数字的shell方法
- [BZOJ1982][POJ1740][Spoj 2021]Moving Pebbles|解题报告
- Page.Response.Buffer与Response.Redirect一起用报错“无法在发送 HTTP 标头之后进行重定向”
- 【转】java中的集合和数组
- PEP 3106 -- Revamping(改进) dict.keys(), .values() and .items()
- 测试social_navigation_layers
- Python 进阶 之 traceback模块
- 基于percona-monitoring-plugins实现Zabbix的MySQL多端口自动发现监控
- debug 英文翻译
- requests库使用socks5代理