新建一个NSString(Time)分类,创建类方法实现时间的转换

.h文件

#import <Foundation/Foundation.h>

@interface NSString (KJTime)
//时间戳--->时间
+(NSString *)transToTime:(NSString *)timsp; //时间戳--->日期
+(NSString *)transToDate:(NSString *)timsp; //时间---->时间戳
+(NSString *)transTotimeSp:(NSString *)time; @end

.m文件

#import "NSString+KJTime.h"

@implementation NSString (KJTime)

//时间戳--->时间
+(NSString *)transToTime:(NSString *)timsp{ NSTimeInterval time=[timsp doubleValue];//如果不使用本地时区,因为时差问题要加8小时 == 28800 sec
NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time]; //实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];//设置本地时区
//设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:@"MM/dd HH:mm"]; NSString *currentDateStr = [dateFormatter stringFromDate: detaildate]; return currentDateStr;
} //时间戳--->日期
+(NSString *)transToDate:(NSString *)timsp{ NSTimeInterval time=[timsp doubleValue];//如果不使用本地时区,因为时差问题要加8小时 == 28800 sec
NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time]; //实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];//设置本地时区
//设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:@"MM月dd日"]; NSString *currentDateStr = [dateFormatter stringFromDate: detaildate]; return currentDateStr;
} //时间---->时间戳
+(NSString *)transTotimeSp:(NSString *)time{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; //设置本地时区
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormatter dateFromString:time];
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];//时间戳
return timeSp;
}
@end

iOS:时间格式化(标准时间转为时间戳、时间戳转为标准时间、时间戳转为日期)的更多相关文章

  1. iOS 时间和时间戳之间转化

    以毫秒为整数值的时间戳转换 时间戳转化为时间NSDate - (NSString *)timeWithTimeIntervalString:(NSString *)timeString { // 格式 ...

  2. 时间戳显示为多少分钟前,多少天前的JS处理,JS时间格式化,时间戳的转换

    var dateDiff = function (timestamp) { // 补全为13位 var arrTimestamp = (timestamp + '').split(''); for ( ...

  3. jsp 自定义标签解决jsp页面中int时间戳的时间格式化问题

    jsp 自定义标签解决jsp页面中int时间戳的时间格式化问题 之前在项目中根据需求,需要自定义标签,经过查询w3c文档,自己也踩了一些坑,特此记录自定义标签的步骤,下面就以我之前的一个例子中的定义一 ...

  4. SQLAlchemy中时间格式化及将时间戳转成对应时间的方法-mysql

    https://blog.csdn.net/guoqianqian5812/article/details/80175866 方法很简答,都是借助于mysql数据库提供的函数将时间格式化方法 func ...

  5. js时间格式化函数,支持Unix时间戳

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  6. golang 时间戳 时间格式化 获取当前时间 timestamp 计算时间差

    获取当前时间 func Now func Now() Time 1 Now returns the current local time. func (Time) UTC func (t Time) ...

  7. PHP获取当前时间、时间戳的各种格式写法汇总[日期时间](转)

    今天写下PHP中,如何通过各种方法 获取当前系统时间.时间戳,并备注各种格式的含义,可灵活变通.1.获取当前时间方法date()很简单,这就是获取时间的方法,格式为:date($format, $ti ...

  8. 【转】PHP获取当前时间、时间戳的各种格式写法汇总[日期时间]

    今天写下php中,如何通过各种方法 获取当前系统时间.时间戳,并备注各种格式的含义,可灵活变通.1.获取当前时间方法date()很简单,这就是获取时间的方法,格式为:date($format, $ti ...

  9. unix时间戳转换成标准时间(c#)

    //---unix时间戳转换成标准时间(c#)---//     /*     string timeStamp = "1144821796";     DateTime dtSt ...

  10. C# 13位时间戳转换成标准时间C#代码

    原地址:https://www.cnblogs.com/yixuehan/p/5559244.html /// <summary> /// 时间戳转换成标准时间 /// </summ ...

随机推荐

  1. java的装饰设计模式

    类似python中的装饰器. 示例: public class Test5 { public static void main(String[] args) { Worker w = new Work ...

  2. python中烦人的锟斤拷(\xef\xbf\xbd)

    首先要知道\xef\xbf\xbd是啥东西 >>> u'\uFFFD'.encode('utf-8') '\xef\xbf\xbd' 由此我们可以知道\xef\xbf\xbd是utf ...

  3. 多路复用I/O模型epoll() 模型 代码实现

    epoll模型 int epoll_create(int maxevent) //创建一个epoll的句柄 然后maxevent表示监听的数目的大小int epoll_ctl(int epollfd, ...

  4. ES Java 客户端

    标签(空格分隔): ES Java 客户端 节点客户端(node client): 节点客户端本身也是一个ES节点(一般不保存数据,不能成为主节点),它能以无数据节点身份加入到集群中.因为它是集群环境 ...

  5. android studio 入门比较好的书籍

    http://blog.csdn.NET/aqi00/article/details/50012511 http://blog.csdn.net/aqi00/article/details/73065 ...

  6. Java学习笔记(十)——xml

    [前面的话] 学习过程中还是效率不够高,老是容易注意力不集中,着急啊.不能在这样了,要好好学习,好好努力. 学习过程中的小知识点总结,基础知识,选择阅读. [xml定义] 定义:可扩展标记语言(英语: ...

  7. AC日记——矩阵取数游戏 洛谷 P1005

    矩阵取数游戏 思路: dp+高精: 代码: #include <bits/stdc++.h> using namespace std; #define ll long long struc ...

  8. Oracle 数据库分页查询的三种方法

    一.Oracle 数据库分页查询的三种方法 1.简介 不能对 rownum 使用 >(大于或等于 1 的数值).>=(大于 1 的数值).=(不等于 1 的数值),否则无结果.所以直接用 ...

  9. ubuntu下基于Anaconda使用Tensorflow

    为了在ubuntu下利用Anaconda使用tensorflow,但在利用conda安装tensorflow,不能在终端,spyder和notebook中直接使用,需要我们进行一定的配置. 1.安装A ...

  10. CSU 2151 集训难度【多标记线段树】

    http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2151 Input 第一行三个数n,m,v0 表示有n名萌新和m次调整,初始时全部萌新的集训难度 ...