ios 如何获得系统时间和日期
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];
ios 如何获得系统时间和日期的更多相关文章
- date 显示或设置系统时间和日期
显示或设置系统时间和日期 date [options] [+format] date [options] [new date] date用来显示系统的时间和日期,超级用户可以使用date来更改系统时钟 ...
- Linux 设置系统时间和日期 API
嵌入式Linux 设置时间和日期 API ,它是busybox要提取的源代码. Linux设置时间和日期的步骤: 1. 设置系统时间和日期: 2. 该系统的时间和日期,同步到硬件. #include ...
- Centos-显示或修改系统时间与日期-date
date 显示或者修改系统时间与日期,只有超级用户才能用date命令设置和修改时间,普通用户只能显示时间 相关参数 -s 设置设置时间,格式为 Y-m-d H:M:S -d 对日期进行运算, + ...
- date---显示或设置系统时间与日期
date命令可以用来显示或设定系统的日期与时间,格式设定为一个加号后接数个标记,其中可用的标记列表如下: 时间方面: %H : 小时(00..23) %M : 分钟(00..59) %p : 显示本地 ...
- C语言中time函数和localtime获取系统时间和日期
可以通过time()函数来获得计算机系统当前的日历时间(Calendar Time),处理日期时间的函数都是以本函数的返回值为基础进行运算. 1. time 函数 返回1970-1-1, 00:00: ...
- ios 获取当前系统时间
1. NSDate + NSDateFormatter NSDate *date = [NSDate date]; NSDateFormatter *format = [[NSDateFormatte ...
- JAVA中获取当前系统时间及格式转换
JAVA中获取当前系统时间 一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; publi ...
- Java 获取当前系统时间方法比较
转载: http://blog.csdn.net/zzjjiandan/article/details/8372617 一. 获取当前系统时间和日期并格式化输出: import java.util.D ...
- 通过java输出当前系统时间
获取当前系统时间和日期并格式化输出: import java.util.Date; import java.text.SimpleDateFormat; public class NowString ...
随机推荐
- 【HDU5785】Interesting [Manacher]
Interesting Time Limit: 30 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description Input Outp ...
- [HNOI2015]部分题解
Day1 T2 [HNOI2015]接水果 风见幽香非常喜欢玩一个叫做 osu!的游戏,其中她最喜欢玩的模式就是接水果.由于她已经DT FC 了The big black, 她觉得这个游戏太简单了, ...
- bzoj 1011 近似估计
开始看这道题的时候没什么思路,后来忍不住看了题解,发现自己真是水啊... 自从学OI来第一次看到用约等的题 首先我们设w[i]为第i个星球的答案,g[i]为第i个星球受到1-g[i]个星球的引力 那么 ...
- 多表查询与pymysql
一.子查询 #1:子查询是将一个查询语句嵌套在另一个查询语句中. #2:内层查询语句的查询结果,可以为外层查询语句提供查询条件. #3:子查询中可以包含:IN.NOT IN.ANY.ALL.EXIST ...
- django执行sql
http://docs.30c.org/djangobook2/chapter10/ def first_names(self, last_name): cursor = connection.cur ...
- Type of flip id
http://www.haskell.org/pipermail/beginners/2011-March/006477.html The point is that the type of id h ...
- Mac-装机
不过大家可别被「命令行」三个字吓到,其实你只需按步骤来,复制粘贴命令即可快速完成,事实上是很简单的. 一.准备工作: 准备一个 8GB 或以上容量的 U 盘,确保里面的数据已经妥善备份好(该过程会抹掉 ...
- WCF发布方式介绍
转载出处:http://blog.csdn.net/fangxing80/article/details/6101790 从VS2005推出WCF以来,WCF逐步取代了Remoting, WebSer ...
- selenium 常用浏览器操作API
package test; import org.openqa.selenium.By;import org.openqa.selenium.Dimension;import org.openqa.s ...
- w3cschool javascript学习
name与Id属性区别 1.我们知道在网页做Post提交时,是以Form(即表单域)为单位进行提交的,一个Form里有若干个表单对象(如<input type="text" ...