iOS - 标准时间与时间戳相互转换
做倒计时后台传的不是时间戳,是时间然后需要与系统时间对比得出时间戳进行倒计时显示
#pragma mark -- 倒计时
// 倒计时时间
NSString* timeStr = task.receiptTime;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // 设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
NSDate* date = [formatter dateFromString:timeStr]; //倒计时时间
NSDate *datenow = [NSDate date];//系统现在时间
//NSString *nowtimeStr = [formatter stringFromDate:datenow];//----------将nsdate按formatter格式转成
//时间转时间戳的方法:
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
NSLog(@"timeSp倒计时时间戳:%@",timeSp);
NSString *timeNow = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
NSLog(@"timeSp系统现在时间戳:%@",timeNow);
double timeInterval = [timeNow doubleValue]-[timeSp doubleValue];
_countDownView.countDownTimeInterval = timeInterval;
NSString* timeStr = @"2019-02-21 16:46:02";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 //设置时区,这个对于时间的处理有时很重要
//例如你在国内发布信息,用户在国外的另一个时区,你想让用户看到正确的发布时间就得注意时区设置,时间的换算.
//例如你发布的时间为2010-01-26 17:40:50,那么在英国爱尔兰那边用户看到的时间应该是多少呢?
//他们与我们有7个小时的时差,所以他们那还没到这个时间呢...那就是把未来的事做了1550738762 NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
NSDate* date = [formatter dateFromString:timeStr]; //------------将字符串按formatter转成nsdate
NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
NSString *nowtimeStr = [formatter stringFromDate:datenow];//----------将nsdate按formatter格式转成 //时间转时间戳的方法:
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
NSLog(@"timeSp:%@",timeSp); //时间戳的值
//时间戳转时间的方法
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:];
NSLog(@"1296035591 = %@",confromTimesp);
NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
NSLog(@"confromTimespStr = %@",confromTimespStr);
//时间戳转时间的方法:
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyyMMddHHMMss"];
NSDate *date = [formatter dateFromString:@""];
NSLog(@"date1:%@",date);
//当前时间转换成秒
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
//现在时间,你可以输出来看下是什么格式
NSDate *datenow = [NSDate date];
//----------将nsdate按formatter格式转成nsstring
NSString *currentTimeString = [formatter stringFromDate:datenow];
NSLog(@"currentTimeString = %@",currentTimeString);
//----------将nsdate按formatter格式转成秒
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
NSLog(@"timeSp = %@",timeSp); //时间秒转换成当前时间
NSDate *date = [NSDate dateWithTimeIntervalSince1970:];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate *localeDate = [date dateByAddingTimeInterval: interval];
NSLog(@"enddate=%@",localeDate);
iOS - 标准时间与时间戳相互转换的更多相关文章
- iOS标准时间与时间戳相互转换
iOS标准时间与时间戳相互转换 (2012-07-18 17:03:34) 转载▼ 标签: ios 时间戳 标准时间 格式 设置 转化 杂谈 分类: iPhone开发 设置时间显示格式: NS ...
- iOS 字典与JSON相互转换
iOS 字典与JSON相互转换 首先简单说一下为什么会写这种幼稚的文章. 现在的网络请求几乎都是AFN完成的,AFN也为我们写了了JSON转换字典的方法,但是不要忘记后台是一个很爱用JSON的人群,H ...
- js sql C#时间、时间戳相互转换
js. sql. C#时间.时间戳相互转换 //1.获取当前时间戳_c# ) / //2.时间戳->时间 C# DateTime b11 = GetTime(");//11位时间戳-& ...
- ios中常用数据类型相互转换
ios中常用数据类型相互转换 //1. NSMutableArray和NSArray互转 // NSArray转为NSMutableArray NSMutableArray *arrM = [arr ...
- 字符串时间与Unix时间戳相互转换
字符串时间与Unix时间戳相互转换 /** * @Author: wangkun * @Date : 2016/1/21 13:43 * @Description : 字符串时间转换为Unix时间戳 ...
- python正常时间和unix时间戳相互转换的方法
python正常时间和unix时间戳相互转换的方法 本文实例讲述了python正常时间和unix时间戳相互转换的方法.分享给大家供大家参考.具体分析如下: 这段代码可以用来转换常规时间格式为unix时 ...
- IOS 获取系统时间戳
IOS 获取系统时间戳常用方法 通用方法有如下三种: NSLog(); NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:]; NSTimeInte ...
- iOS:时间格式化(标准时间转为时间戳、时间戳转为标准时间、时间戳转为日期)
新建一个NSString(Time)分类,创建类方法实现时间的转换 .h文件 #import <Foundation/Foundation.h> @interface NSString ( ...
- Python3 日期与时间戳相互转换
开发中经常会对时间格式处理,对于时间数据,比如2019-02-28 10:23:29,有时需要日期与时间戳进行相互转换,在Python3中主要用到time模块,相关的函数如下: 其中unix_time ...
随机推荐
- 奇怪吸引子---NewtonLeipnik
奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...
- webservice-整理
webservice-整理 RPC与WebService的区别:https://blog.csdn.net/defonds/article/details/71641634 http://www.di ...
- [Python设计模式] 第8章 学习雷锋好榜样——工厂方法模式
github地址:https://github.com/cheesezh/python_design_patterns 简单工厂模式 v.s. 工厂方法模式 以简单计算器为例,对比一下简单工厂模式和工 ...
- document.domain 实现跨域
该方式只能用于二级域名相同的情况下,比如 a.test.com 和 b.test.com 适用于该方式. 只需要给页面添加 document.domain = 'test.com' 表示二级域名都相同 ...
- 【LeetCode】238. Product of Array Except Self
Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...
- 译 5. Spring使用JDBC访问关系数据
本指南将引导您完成使用Spring访问关系数据的过程.原文阅读 1. 你将构建什么? 您将使用Spring的JdbcTemplate构建一个应用程序来访问存储在关系数据库中的数据. 2. 你需要准备什 ...
- 【Ubuntu】xrdp完美实现Windows远程访问Ubuntu 16.04
步骤一.下载TigerVNC Server软件包 下载地址:Tigervnc Deb软件包(适用于Ubuntu 16.04.1 - 64位) 步骤二. 安装TigerVNC Server软件包 1.打 ...
- 使用自定义的 Manager
class CategoryManager(models.Manager): """ A manager that adds an "active()" ...
- java中job运行时间
long startTime=System.currentTimeMillis();//记录开始时间 method();//此处为你调用的方法 long endTime=System.currentT ...
- 主机使用代理上网,虚拟机centos7如何连外网
简介 今天任务不多,就想在学一些fastdfs,然后发现公司是通过代理上外网的,这里记录下,主机使用代理上网,虚拟机centos7如何连外网 vi /etc/yum.conf 加上 proxy=ht ...