iOS开发NSDate、NSString、时间戳之间的转化
//将UTCDate(世界标准时间)转化为当地时区的标准Date(钟表显示的时间)
//NSDate *date = [NSDate date]; 2018-03-27 06:54:41 +0000
//转化后:2018-03-27 14:54:41 +0000
-(NSDate *)getLocalDateFromUTCDate:(NSDate *)UTCDate{ NSTimeZone *tz = [NSTimeZone defaultTimeZone];
NSInteger seconds = [tz secondsFromGMTForDate: UTCDate];
return [NSDate dateWithTimeInterval: seconds sinceDate: UTCDate]; } //将当地时区的标准Date转化为UTCDate
//当前当地的标准时间:2018-03-27 14:54:41 +0000
//转化为世界标准时间:2018-03-27 06:54:41 +0000
-(NSDate *)getUTCDateFromLocalDate:(NSDate *)LocalDate{ NSTimeZone *tz = [NSTimeZone defaultTimeZone];
NSInteger seconds = -[tz secondsFromGMTForDate: LocalDate];
return [NSDate dateWithTimeInterval: seconds sinceDate: LocalDate]; } //根据UTCDate获取当前时间字符串(钟表上显示的时间)
//输入:[NSDate date] 2018-03-27 07:44:05 +0000
//输出:2018-03-27 15:44:05
-(NSString *)localStringFromUTCDate:(NSDate *)UTCDate{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
[dateFormatter setTimeZone:tz];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString* result=[dateFormatter stringFromDate:UTCDate];
return result; } //根据UTC字符串获取当前时间字符串(钟表上显示的时间)
//输入:2018-03-27 07:44:05
//输出:2018-03-27 15:44:05
-(NSString *)localStringFromUTCString:(NSString *)UTCString{ //先将UTC字符串转为UTCDate;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:tz];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *UTCDate = [dateFormatter dateFromString:UTCString]; [dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
NSString* result = [dateFormatter stringFromDate:UTCDate];
return result;
} //将当前时间字符串转为UTCDate
-(NSDate *)UTCDateFromLocalString:(NSString *)localString{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:localString];
return date;
} //将当前时间字符串转为UTC字符串
-(NSString *)UTCStringFromLocalString:(NSString *)localString{ NSDate *date = [self UTCDateFromLocalString:localString];
NSString *string = [NSString stringWithFormat:@"%@",date];
NSString *result = [string substringToIndex:string.length-];
return result; } //UTCDate转UTC字符串
-(NSString *)UTCStringFromUTCDate:(NSDate *)UTCDate{ NSDateFormatter *dataFormatter = [[NSDateFormatter alloc]init];
[dataFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dataFormatter setTimeZone:tz];
NSString *UTCString = [dataFormatter stringFromDate:UTCDate];
return UTCString; } //将当前时间(UTCDate)转为时间戳
-(NSString *)timeStampFromUTCDate:(NSDate *)UTCDate{ NSTimeInterval timeInterval = [UTCDate timeIntervalSince1970];
// *1000,是精确到毫秒;这里是精确到秒;
NSString *result = [NSString stringWithFormat:@"%.0f",timeInterval];
return result; } //当前时间字符串(钟表上显示的时间)转为时间戳
-(NSString *)timeStamapFromLocalString:(NSString *)localString{ //先转为UTCDate
NSDate *UTCDate = [self UTCDateFromLocalString:localString];
NSString *timeStamap = [self timeStampFromUTCDate:UTCDate];
return timeStamap; } //将UTCString转为时间戳
-(NSString *)timeStamapFromUTCString:(NSString *)UTCString{ //先将UTC字符串转为UTCDate;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:tz];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *UTCDate = [dateFormatter dateFromString:UTCString]; NSString *timeStamap = [self timeStampFromUTCDate:UTCDate];
return timeStamap;
} //时间戳转UTCDate
-(NSDate *)UTCDateFromTimeStamap:(NSString *)timeStamap{ NSTimeInterval timeInterval=[timeStamap doubleValue];
// /1000;传入的时间戳timeStamap如果是精确到毫秒的记得要/1000
NSDate *UTCDate=[NSDate dateWithTimeIntervalSince1970:timeInterval];
return UTCDate; } 若是想要将时间戳转为字符串,可根据获得的UTCDate再进行转化
iOS开发NSDate、NSString、时间戳之间的转化的更多相关文章
- iOS开发拓展篇—应用之间的跳转和数据传递
iOS开发拓展篇—应用之间的跳转和数据传 说明:本文介绍app如何打开另一个app,并且传递数据. 一.简单说明 新建两个应用,分别为应用A和应用B. 实现要求:在appA的页面中点击对应的按钮,能够 ...
- javascript中日期格式与时间戳之间的转化
日期格式与时间戳之间的转化 一:日期格式转化为时间戳 function timeTodate(date) { var new_str = date.replace(/:/g,'-'); new_str ...
- iOS开发-NSDate使用
时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)至当前时间的总秒数.它也被称为 Unix 时间戳(Unix Timestamp). 下面是iOS中时间戳 与 时间之间的转换方法: ...
- ios开发--NSDate与NSDateFormatter的相关用法【转】
原文地址:http://blog.sina.com.cn/s/blog_91ff71c0010188u9.html 1.NSDateFormatter配合NSDate与NSString之间的转化 N ...
- iOS开发 当前时间 时间戳 转换
1.今天在做一个webservice的接口的时候,被要求传一个时间戳过去,然后就是开始在Google上找 2.遇到两个问题,一,当前时间转化为时间戳,二,获取的当前时间和系统的时间相差8个小时 一,转 ...
- iOS开发NSDate详解
1. 用于创建NSDate实例的类方法有 + (id)date; 返回当前时间 + (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs; 返回以 ...
- iOS开发系列-应用程序之间跳转
概述 常见的涉及到应用程序之间的跳转场景,比如社交分享.支付宝.微信支付.链接跳转到应用. 在iOS中应用跳转的本质:打开一个应用只需要拿到对应应用的URL即可. 统一资源定位符 URL(统一资源定位 ...
- iOS开发——NSDate(待续...)
1.获取当前系统时间,毫秒级 - (void)viewDidLoad { [super viewDidLoad]; NSString *currentTime = [self getCurrentTi ...
- iOS开发之获取时间戳方法
// 得到当前本地时间,13位,整形 + (long long)gs_getCurrentTimeToMilliSecond { double currentTime = [[NSDate date] ...
随机推荐
- 机器学习技法笔记:01 Linear Support Vector Machine
Roadmap Course Introduction Large-Margin Separating Hyperplane Standard Large-Margin Problem Support ...
- vue 自学笔记(5) 列表渲染
列表渲染 一:v-for 指令 当我们涉及到列表渲染数据的时候,不可能做一个重复的工作去不停的一个一个的渲染每一项列表.并且列表数据的表现,比如从后端请求过来的数据,不可能是一个一个的单独的 JSON ...
- LeetCode--No.007 Reverse Integer
7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...
- FF笔试题整理
一.选择题 1.怎样能唯一确定一颗二叉树? [解析] 只要知道中序遍历顺序,再加上其余两个遍历中任意一个都可以唯一确定一个二叉树.如果不知道中序遍历顺序,则无法确定. [反例] A-B-C,A是跟,B ...
- OPC安装-配置(http://www.mabotech.com)
1.使用opc,需要在机器上安装OPC运行环境.opc运行环境包含:opc_aeps.dll.opccomn_ps.dll.opcdaauto.dll.OpcEnum.exe.opcproxy.dll ...
- 关于 Spring Security 5 默认使用 Password Hash 算法
账户密码存储的安全性是一个很老的话题,但还是会频频发生,一般的做法是 SHA256(userInputpwd+globalsalt+usersalt) 并设置密码时时要求长度与大小写组合,一般这样设计 ...
- mysql 开发进阶篇系列 40 mysql日志之二进制日志下以及查询日志
一.binlog 二进制其它选项 在二进制日志记录了数据的变化过程,对于数据的完整性和安全性起着非常重要作用.在mysql中还提供了一些其它参数选项,来进行更小粒度的管理. 1.1 binlog-do ...
- 全网最详细的实用的搜索工具Listary和Everything对比的区别【堪称比Everything要好】(图文详解)
不多说,直接上干货! 引言 无论是工作还是科研,我们都希望工作既快又好,然而大多数时候却迷失在繁杂的重复劳动中,久久无法摆脱繁杂的事情. 你是不是曾有这样一种想法:如果我有哆啦A梦的口袋,只要拿出 ...
- php使用memcached缓存总结
1. 查询多行记录,以sql的md5值为key,缓存数组(个人觉得最好用的方法) $mem = new Memcache(); $mem->connect('127.0.0.1',11211); ...
- Django 学习笔记(二) --- HTML 模版
人生苦短 ~ Tips:仅适用于 Python 3+(反正差别不大,py2 改改也能用).因为据 Python 之父 Guido van Rossum 说会在 2020 年停止对 Python 2 的 ...