iOS 本地时间 / UTC时间 / 时间戳等操作 / 获取当前年月日
//获得当前时间并且转为字符串

- (NSString *)dateTransformToTimeString
{
NSDate *currentDate = [NSDate date];//获得当前时间为UTC时间 2014-07-16 07:54:36 UTC (UTC时间比标准时间差8小时)
//转为字符串
NSDateFormatter*df = [[NSDateFormatter alloc]init];//实例化时间格式类
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//格式化
//2014-07-16 07:54:36(NSString类)
NSString *timeString = [df stringFromDate:currentDate];
return timeString;
}

//获取当前时间转为时间戳
- (NSString *)dateTransformToTimeSp
{
UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;//客户端当前13位毫秒级时间戳
NSString *timeSp = [NSString stringWithFormat:@"%llu",recordTime];//时间戳转字符串(13位毫秒级时间戳字符串)
return timeSp;
}

1 //时间戳字符串1469193006001(毫秒)1469193006.001(毫秒,1469193006001234(微秒)1469193006.001234(微秒)转 UTC时间2016-08-11T07:00:55.611Z
2 - (NSString *)timespToUTCFormat:(NSString *)timesp
3 {
4 NSString *timeString = [timesp stringByReplacingOccurrencesOfString:@"." withString:@""];
5 if (timeString.length >= 10) {
6 NSString *second = [timeString substringToIndex:10];
7 NSString *milliscond = [timeString substringFromIndex:10];
8 NSString * timeStampString = [NSString stringWithFormat:@"%@.%@",second,milliscond];
9 NSTimeInterval _interval=[timeStampString doubleValue];
10 NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
11
12 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
13 NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
14 [dateFormatter setTimeZone:timeZone];
15 [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
16 NSString *dateString = [dateFormatter stringFromDate:date];
17
18 return dateString;
19 }
20 return @"";
21 }

//13位时间戳1469193006001(毫秒)转 系统时间2016-08-11 08:55:36

1 + (NSString *)timespToYMDFormat:(NSNumber *)timesp
2 {
3 NSString *stime = [timesp stringValue];
4 NSTimeInterval time = [[stime substringToIndex:10] doubleValue];
5 NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
6 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
7 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
8
9 return [dateFormatter stringFromDate: detaildate];
10 }

//时间转时间戳的方法:sendDate为NSDate类
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[sendDate timeIntervalSince1970]];
如果只获取当前的年月日,用NSDate 直接截取是不对的,以下方法提供了获取当前的年月日等等
// 获取代表公历的NSCalendar对象
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
// 获取当前日期
// 定义一个时间字段的旗标,指定将会获取指定年、月、日、时、分、秒的信息
unsigned unitFlags = NSCalendarUnitYear |
NSCalendarUnitMonth | NSCalendarUnitDay |
NSCalendarUnitHour | NSCalendarUnitMinute |
NSCalendarUnitSecond | NSCalendarUnitWeekday;
// 获取不同时间字段的信息
NSDateComponents* comp = [gregorian components: unitFlags
fromDate:localeDate];
NSInteger year = comp.year; //下面是可以获取的内容 //
@property NSInteger era;
@property NSInteger year;
@property NSInteger month;
@property NSInteger day;
@property NSInteger hour;
@property NSInteger minute;
@property NSInteger second;
@property NSInteger nanosecond NS_AVAILABLE(10_7, 5_0);
@property NSInteger weekday;
@property NSInteger weekdayOrdinal;
@property NSInteger quarter NS_AVAILABLE(10_6, 4_0);
@property NSInteger weekOfMonth NS_AVAILABLE(10_7, 5_0);
@property NSInteger weekOfYear NS_AVAILABLE(10_7, 5_0);
@property NSInteger yearForWeekOfYear NS_AVAILABLE(10_7, 5_0);
iOS 本地时间 / UTC时间 / 时间戳等操作 / 获取当前年月日的更多相关文章
- 获取当前时间UTC时间的下一个15分钟时间点
ZonedDateTime zdt = ZonedDateTime.now(ZoneOffset.UTC); int now15Minute = zdt.getMinute() / P15MINUTE ...
- mysql 日期操作 增减天数、时间转换、时间戳(转)
转自http://www.cnblogs.com/wenzichiqingwa/archive/2013/03/05/2944485.html http://hi.baidu.com/juntao_l ...
- mysql 日期操作 增减天数、时间转换、时间戳(转换)
http://hi.baidu.com/juntao_li/item/094d78c6ce1aa060f6c95d0b MySQL datediff(date1,date2):两个日期相减 date1 ...
- java UTC时间和local时间相互转换
java UTC时间和local时间相互转换 1.local时间转UTC时间 /** * local时间转换成UTC时间 * @param localTime * @return */ public ...
- C#实现UTC时间与Datetime转换
为了便于传输,通信过程中传输的都是:当前时间跟标准时间相隔的秒数,并且是以16进制字节的形式传输的. public double ConvertDateTimeInt(System.DateTime ...
- C# 获取utc时间,以及utc datetime 互相转化
C# 获取utc时间,以及utc datetime 互相转化 大部分源于http://blog.sina.com.cn/s/blog_4c6e822d0102dsdz.html 刚开始学习一点C# ...
- MySQL 获得当前日期时间(以及时间的转换)
1.1 获得当前日期+时间(date + time)函数:now() 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下面的函数: current_timestamp() curr ...
- iOS 本地时间、UTC时间、时间戳等操作、获取当前年月日
//获得当前时间并且转为字符串 - (NSString *)dateTransformToTimeString { NSDate *currentDate = [NSDate date];//获得当前 ...
- [转帖]UTC时间、GMT时间、本地时间、Unix时间戳
UTC时间.GMT时间.本地时间.Unix时间戳 https://www.cnblogs.com/xwdreamer/p/8761825.html 引用: https://blog.csdn.net/ ...
随机推荐
- ROS标定IDS相机
参考 ROS 相机标定http://blog.csdn.net/ArtistA/article/details/51125560 ROS里的标定程序只要使用了OPNCV的标定程序: opencv 相机 ...
- golang之map数据类型
先上代码…… package main import "fmt" func testMap() { //两种声明map方式,切记,必须初始化才能用,否则panic //var a ...
- 第七章 资源在Windows编程中的应用 P157 7-8
资源在基于SDK的程序设计中的应用实验 一.实验目的 1.掌握各种资源的应用及资源应用的程序设计方法. 二.实验内容及步骤 实验任务 1.熟悉菜单资源的创建过程: 2.熟悉位图资源的创建: 3.熟 ...
- Spring JMX之二:远程访问MBean
虽然最初的JMX规范提及了通过MBean进行应用的远程管理,但是它并没有定义实际的远程 访问协议或API.因此,会由JMX供应商定义自己的JMX远程访问解决方案,但这通常又是专 有的. 为了满足以标准 ...
- 关于流程图设计,你需要Get的几点必备知识
流程图(Flow Chart)这个概念对很多人来说并不陌生,但如果让你定义或者举例说明什么是产品流程图,恐怕还是有难度的.或许诸如“用户体验”.“交互设计”.“逻辑关系”等词会像走马灯般闪现在你的脑海 ...
- 用MapReduce读HBase写MongoDB样例
1.版本信息: Hadoop版本:2.7.1 HBase版本:1.2.1 MongDB版本:3.4.14 2.HBase表名及数据: 3.Maven依赖: <dependency> < ...
- 04 Python入门学习-流程控制(if else elif while for)
一:流程控制if 语法一: if 条件: code1 code2 code3 ... age = 20 height = 170 weight = 60 sex = 'female' is_beaut ...
- UVa 10603 Fill (暴力BFS+优先队列)
题意:给定4个数,a,b,c,d,分别代表空杯子容积为a,b,一个盛满水的杯子容积为c,让你不断倒水,找一个dd,是不是存在某个时刻, 某个杯子里的水dd,和d相同,或者无限接近.让求最少的倒水量和d ...
- WCF双工通信笔记
1,Dupex(双工) MEP(消息交换模式),服务端回调(Callback)客户端操作 2,客户端调用服务时,附加上一个回调对象(InstanceContext).服务端处理服务请求时,通过该回调对 ...
- (转)SQL Server内存遭遇操作系统进程压榨案例
原文地址:http://www.cnblogs.com/zc_0101/p/3592259.html 场景: 最近一台DB服务器偶尔出现CPU报警,我的邮件报警阈(请读yù)值设置的是15%,开始时没 ...