//将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、时间戳之间的转化的更多相关文章

  1. iOS开发拓展篇—应用之间的跳转和数据传递

    iOS开发拓展篇—应用之间的跳转和数据传 说明:本文介绍app如何打开另一个app,并且传递数据. 一.简单说明 新建两个应用,分别为应用A和应用B. 实现要求:在appA的页面中点击对应的按钮,能够 ...

  2. javascript中日期格式与时间戳之间的转化

    日期格式与时间戳之间的转化 一:日期格式转化为时间戳 function timeTodate(date) { var new_str = date.replace(/:/g,'-'); new_str ...

  3. iOS开发-NSDate使用

    时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)至当前时间的总秒数.它也被称为 Unix 时间戳(Unix Timestamp). 下面是iOS中时间戳 与 时间之间的转换方法: ...

  4. ios开发--NSDate与NSDateFormatter的相关用法【转】

    原文地址:http://blog.sina.com.cn/s/blog_91ff71c0010188u9.html 1.NSDateFormatter配合NSDate与NSString之间的转化  N ...

  5. iOS开发 当前时间 时间戳 转换

    1.今天在做一个webservice的接口的时候,被要求传一个时间戳过去,然后就是开始在Google上找 2.遇到两个问题,一,当前时间转化为时间戳,二,获取的当前时间和系统的时间相差8个小时 一,转 ...

  6. iOS开发NSDate详解

    1. 用于创建NSDate实例的类方法有 + (id)date; 返回当前时间 + (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs; 返回以 ...

  7. iOS开发系列-应用程序之间跳转

    概述 常见的涉及到应用程序之间的跳转场景,比如社交分享.支付宝.微信支付.链接跳转到应用. 在iOS中应用跳转的本质:打开一个应用只需要拿到对应应用的URL即可. 统一资源定位符 URL(统一资源定位 ...

  8. iOS开发——NSDate(待续...)

    1.获取当前系统时间,毫秒级 - (void)viewDidLoad { [super viewDidLoad]; NSString *currentTime = [self getCurrentTi ...

  9. iOS开发之获取时间戳方法

    // 得到当前本地时间,13位,整形 + (long long)gs_getCurrentTimeToMilliSecond { double currentTime = [[NSDate date] ...

随机推荐

  1. Swift5 语言指南(九) 闭包

    闭包是自包含的功能块,可以在代码中传递和使用.Swift中的闭包类似于C和Objective-C中的块以及其他编程语言中的lambdas. 闭包可以从定义它们的上下文中捕获和存储对任何常量和变量的引用 ...

  2. HTTP请求时间参数设置

    1. JSON 2019-01-18 18:36:35 2. Postman 2019/01/18 18:36:35

  3. HoloLens开发手记-全息Hologram

    HoloLens使我们可以通过周边世界的光线和声音来创建全息场景和物体,使得它们像真实物体那样.全息场景能够响应你的凝视.手势和语音指令,同时还会和你周边世界的表面交互.借助全息场景,你可以在周边世界 ...

  4. fast.ai(零)windows + pytorch 0.4

    一.下载 git clone https://github.com/fastai/fastai.git 或者直接下载下来 二.安装pytorch 去官网安装建议安装即可 https://pytorch ...

  5. Liferay7 BPM门户开发之4: Activiti事件处理和监听Event handlers

    事件机制从Activiti 5.15开始引入,这非常棒,他可以让你实现委托. 可以通过配置添加事件监听器,也可以通过Runtime API加入注册事件. 所有的事件参数子类型都来自org.activi ...

  6. Spark Core

    Spark Core    DAG概念        有向无环图        Spark会根据用户提交的计算逻辑中的RDD的转换(变换方法)和动作(action方法)来生成RDD之间的依赖关系,同时 ...

  7. vue项目中在同一页面多次引入同一个echarts图表的自适应问题

    在父组件页面引入两次该图表子组件显示的效果: 由于是百分比宽高,所以在窗口发生变化时,需要让图表也跟着自适应,所以才出现了本次讨论的问题啦. 先看下完整的图表子组件代码(在父组件就是直接引入,不需要传 ...

  8. iOS app 支持HTTPS iOS开发者相关

    2016年12月21日更新开发者中心链接https://developer.apple.com/news/?id=12212016b该链接是苹果昨天刚在官网给的正式回复 如下: App Transpo ...

  9. 常见HTTP状态码及URL编码表

    常见HTTP状态码 1xx: 信息          (用于表示临时响应并需要请求者执行操作才能继续的状态代码) 消息: 描述: 100 Continue 服务器仅接收到部分请求,但是一旦服务器并没有 ...

  10. 基于vue2.0实现仿百度前端分页效果(二)

    前言 上篇文章中,已经使用vue实现前端分页效果,这篇文章我们单独将分页抽离出来实现一个分页组件 先看实现效果图 代码实现 按照惯例,我们在冻手实现的时候还是先想一想vue实现组件的思路 1.需要提前 ...