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] ...
随机推荐
- nginx反向代理转发apache配置 之 cookie去哪儿了?
在公司接手了个微信项目,由于微信环境下访问网站需要使用对外开放的域名,所以有相关问题,都是直接运维同事帮忙处理. 原理是这样: 方案一: 1. 将域名解析指向测试服务器的地址: 2. 开放相关端口访问 ...
- Python模块——logging模块
logging模块简介 logging模块定义的函数和类为应用程序和库的开发实现了一个灵活的事件日志系统.logging模块是Python的一个标准库模块, 由标准库模块提供日志记录API的关键好处是 ...
- 关于springboot aop 俩次调用的问题 aop多次调用
由于我在springboot 启动类中 给我的切面类进行了赋值 即@Bean 然而我在切面类中加了@Component 导致 springboot 注入了俩个 bean 所以导致 aop 多次执行 ...
- 829. 连续整数求和-leetcode
题目:给定一个正整数 N,试求有多少组连续正整数满足所有数字之和为 N? 示例 1: 输入: 5 输出: 2 解释: 5 = 5 = 2 + 3,共有两组连续整数([5],[2,3])求和后为 5. ...
- leetcode — two-sum-iii-data-structure-design
import java.util.HashMap; import java.util.Map; /** * Source : https://oj.leetcode.com/problems/two- ...
- C语言第六讲,数组
C语言第六讲,数组 一丶什么是数组 数组,就是一整块的连续内存空间. 且类型都是一样的.大小一样 比如: 1.1数组元素的访问 我们要访问数组,例如上面,我们访问元算2,元素3等等怎么访问.. 比如有 ...
- Netty精粹之轻量级内存池技术实现原理与应用
摘要: 在Netty中,通常会有多个IO线程独立工作,基于NioEventLoop的实现,每个IO线程负责轮询单独的Selector实例来检索IO事件,当IO事件来临的时候,IO线程开始处理IO事件. ...
- 2017年最新20个轻量的 JavaScript 库和插件
下面这个列表中的免费 JavaScript 插件都是今年发布的,没有臃肿的一体化的框架,它们提供轻量级的解决方案,帮助 Web 开发过程更容易和更快.提供的插件可以创建滑块.响应式菜单.模态窗口.相册 ...
- Java设计模式学习记录-组合模式
前言 今天要介绍的设计模式是组合模式,组合模式也是结构型设计模式的一种,它主要体现了整体与部分的关系,其典型的应用就是树形结构.组合是一组对象,其中的对象可能包含一个其他对象,也可能包含一组其他对象. ...
- Mysql 存储过程实例详解
存储过程和函数是事先经过编译并存储在数据库中的一段SQL语句的集合,存储和和函数的区别在于函数必须有返回值,而存储过程没有,存储过程的参数可以使用IN.OUT.INOUT类型,而函数的参数只能是IN类 ...