iphone 如何获得系统时间和日期

代码如下:

#import <time.h>

1。获得当前的系统时间和日期

  1. //获得系统时间
  2. NSDate *  senddate=[NSDate date];
  3. NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
  4. [dateformatter setDateFormat:@"HH:mm"];
  5. NSString *  locationString=[dateformatter stringFromDate:senddate];
  6. //[dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
  7. //NSString *  morelocationString=[dateformatter stringFromDate:senddate];
  8. //获得系统日期
  9. NSCalendar  * cal=[NSCalendar  currentCalendar];
  10. NSUInteger  unitFlags=NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit;
  11. NSDateComponents * conponent= [cal components:unitFlags fromDate:senddate];
  12. NSInteger year=[conponent year];
  13. NSInteger month=[conponent month];
  14. NSInteger day=[conponent day];
  15. NSString *  nsDateString= [NSString  stringWithFormat:@"%4d年%2d月%2d日",year,month,day];
  16. [dateformatter release];

2。从字符串来获得NSDate

  1. string  strYear="1988";
  2. string  strMonth="09";
  3. string  strDay="18";
  4. string  strHour="5";
  5. string  strMinutes="18";
  6. string  strSec="20";
  7. morelocationString=[NSString stringWithFormat:@"%s-%s-%s-%s-%s-%s",strYear.c_str(),strMonth.c_str(),
  8. strDay.c_str(),strHour.c_str(),strMinutes.c_str(),strSec.c_str()];
  9. //根据时间字符串获得NSDate
  10. NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
  11. [dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
  12. NSDate  * oldDate=[dateformatter dateFromString:morelocationString];

通过上面的代码,获得了NSDate。

3。 从GMT时间,得到本地时间

  1. NSDate  * oldDate=[dateformatter dateFromString:morelocationString];
  2. NSTimeInterval  timeZoneOffset=[[NSTimeZone systemTimeZone] secondsFromGMT];
  3. NSDate  * newDate=[oldDate dateByAddingTimeInterval:timeZoneOffset];

最新文章

  1. cordova plugin数据传递概要
  2. WPF:将HTML RGB颜色值转化为Color对象的两种方式
  3. Qt设置horizontal line 和vertical line 的颜色
  4. API断点大全
  5. Oracle系列之异常处理
  6. Android学习笔记(六)Fragment的生命周期
  7. centos6 Cacti部署文档
  8. 从零开始制作Minecraft启动器(C++开源)
  9. Cocos2d-x C++调用Android弹出提示框
  10. Java下获取可用CPU数
  11. 51 NOD 1238 最小公倍数之和 V3
  12. Problem - 1062 http://acm.hdu.edu.cn/showproblem.php?pid=1062
  13. lambda 和 iterable
  14. Win10系列:VC++ XML文件解析
  15. IEnumerator和IEnumerable详解
  16. 行为类模式(十一):访问者(Visitor)
  17. Java虚拟机(三):Java 类的加载机制
  18. phpStudy7——MySql数据库的增删查改
  19. jquery记忆笔记
  20. thinkphp5集成微信支付【公众号支付】快捷路径

热门文章

  1. 从一段字符串中去除数字的shell方法
  2. [BZOJ1982][POJ1740][Spoj 2021]Moving Pebbles|解题报告
  3. Page.Response.Buffer与Response.Redirect一起用报错“无法在发送 HTTP 标头之后进行重定向”
  4. 【转】java中的集合和数组
  5. PEP 3106 -- Revamping(改进) dict.keys(), .values() and .items()
  6. 测试social_navigation_layers
  7. Python 进阶 之 traceback模块
  8. 基于percona-monitoring-plugins实现Zabbix的MySQL多端口自动发现监控
  9. debug 英文翻译
  10. requests库使用socks5代理