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];

ios 如何获得系统时间和日期的更多相关文章

  1. date 显示或设置系统时间和日期

    显示或设置系统时间和日期 date [options] [+format] date [options] [new date] date用来显示系统的时间和日期,超级用户可以使用date来更改系统时钟 ...

  2. Linux 设置系统时间和日期 API

    嵌入式Linux 设置时间和日期 API ,它是busybox要提取的源代码. Linux设置时间和日期的步骤: 1. 设置系统时间和日期: 2. 该系统的时间和日期,同步到硬件. #include ...

  3. Centos-显示或修改系统时间与日期-date

    date 显示或者修改系统时间与日期,只有超级用户才能用date命令设置和修改时间,普通用户只能显示时间 相关参数 -s 设置设置时间,格式为 Y-m-d H:M:S -d    对日期进行运算, + ...

  4. date---显示或设置系统时间与日期

    date命令可以用来显示或设定系统的日期与时间,格式设定为一个加号后接数个标记,其中可用的标记列表如下: 时间方面: %H : 小时(00..23) %M : 分钟(00..59) %p : 显示本地 ...

  5. C语言中time函数和localtime获取系统时间和日期

    可以通过time()函数来获得计算机系统当前的日历时间(Calendar Time),处理日期时间的函数都是以本函数的返回值为基础进行运算. 1. time 函数 返回1970-1-1, 00:00: ...

  6. ios 获取当前系统时间

    1. NSDate + NSDateFormatter NSDate *date = [NSDate date]; NSDateFormatter *format = [[NSDateFormatte ...

  7. JAVA中获取当前系统时间及格式转换

    JAVA中获取当前系统时间   一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; publi ...

  8. Java 获取当前系统时间方法比较

    转载: http://blog.csdn.net/zzjjiandan/article/details/8372617 一. 获取当前系统时间和日期并格式化输出: import java.util.D ...

  9. 通过java输出当前系统时间

    获取当前系统时间和日期并格式化输出: import java.util.Date; import java.text.SimpleDateFormat; public class NowString ...

随机推荐

  1. 【HDU5785】Interesting [Manacher]

    Interesting Time Limit: 30 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description Input Outp ...

  2. [HNOI2015]部分题解

    Day1 T2 [HNOI2015]接水果 风见幽香非常喜欢玩一个叫做 osu!的游戏,其中她最喜欢玩的模式就是接水果.由于她已经DT FC 了The big black,  她觉得这个游戏太简单了, ...

  3. bzoj 1011 近似估计

    开始看这道题的时候没什么思路,后来忍不住看了题解,发现自己真是水啊... 自从学OI来第一次看到用约等的题 首先我们设w[i]为第i个星球的答案,g[i]为第i个星球受到1-g[i]个星球的引力 那么 ...

  4. 多表查询与pymysql

    一.子查询 #1:子查询是将一个查询语句嵌套在另一个查询语句中. #2:内层查询语句的查询结果,可以为外层查询语句提供查询条件. #3:子查询中可以包含:IN.NOT IN.ANY.ALL.EXIST ...

  5. django执行sql

    http://docs.30c.org/djangobook2/chapter10/ def first_names(self, last_name): cursor = connection.cur ...

  6. Type of flip id

    http://www.haskell.org/pipermail/beginners/2011-March/006477.html The point is that the type of id h ...

  7. Mac-装机

    不过大家可别被「命令行」三个字吓到,其实你只需按步骤来,复制粘贴命令即可快速完成,事实上是很简单的. 一.准备工作: 准备一个 8GB 或以上容量的 U 盘,确保里面的数据已经妥善备份好(该过程会抹掉 ...

  8. WCF发布方式介绍

    转载出处:http://blog.csdn.net/fangxing80/article/details/6101790 从VS2005推出WCF以来,WCF逐步取代了Remoting, WebSer ...

  9. selenium 常用浏览器操作API

    package test; import org.openqa.selenium.By;import org.openqa.selenium.Dimension;import org.openqa.s ...

  10. w3cschool javascript学习

    name与Id属性区别 1.我们知道在网页做Post提交时,是以Form(即表单域)为单位进行提交的,一个Form里有若干个表单对象(如<input type="text" ...