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] ...
随机推荐
- [Swift]键盘遮挡控件
键盘遮挡控件: super.viewDidLoad(){ // Do any additional setup after loading the view, typically from a nib ...
- IDEA Exception in thread "main" java.lang.ClassNotFoundException: com.streamax.servicecore.business.FileManageServApplication
[参考文章]:intelij idea: Exception in thread "main" java.lang.ClassNotFoundException 1. 报错信息 2 ...
- oracle对sum出来的数字进行非空补0处理
oracle在使用函数计算式会遇到这样的情况:例如sum函数 如果计算的sum值为null,则用0替代 方法1(便于理解): select when sum(c.num) is null then ...
- Python——pytessercat识别简单的验证码
什么是验证码 验证码(CAPTCHA)是“Completely Automated Public Turing test to tell Computersand Humans Apart” (全自动 ...
- “玲珑杯”ACM比赛 Round #18---图论你先敲完模板(DP+思维)
题目链接 DESCRIPTION INPUT OUTPUT SAMPLE INPUT 2 3 2 3 5 7 3 10 3 5 7 SAMPLE OUTPUT 12 26 HINT 官方题解: 代码如 ...
- Python中通过threshold创建mask
[code] import numpy as np threshold=2 a=np.array([[1,2,3],[3,4,5]]) b=a>threshold print("a=& ...
- 关于jquery中prev()和next()的用法
用prev()和next()方法动态的添加class.以达到当前元素的前面几个元素或后面的几个元素添加class <body> <ul> <li>1</li& ...
- JavaWeb学习 (四)————Http协议
一.什么是HTTP协议 HTTP是hypertext transfer protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的 ...
- 金山wps面经
前言: 金山wps笔试是好久之前的了,忘记具体几号了.当时在华师参加的宣讲会,然后线下笔试通过了, 昨天(4月2号通知现场面试).今天是在华工酒店进行面试的,一二面一起进行的 一面: 1: 自我介绍 ...
- 【F12】chrome浏览器中 F12 功能的简单介绍
chrome浏览器中 F12 功能的简单介绍 由于F12是前端开发人员的利器,所以我自己也在不断摸索中,查看一些博客和资料后,自己总结了一下来帮助自己理解和记忆,也希望能帮到有需要的小伙伴,嘿嘿! 首 ...