当前位置:首页 > 未命名 > 正文内容

OC时间戳转时间方法系列

9年前 (2017-06-06)未命名
// 时间戳转时间
+ (NSString *)timeStampConv:(NSString *)timestamp;
// 秒转换成00:00:00
+ (NSString *)timeSecConv:(NSString *)second;
// 字符串转时间
+ (NSDate *)stringToDate:(NSString *)dateString withDateFormat:(NSString *)format;
// 时区转换
+ (NSDate *)worldTimeToChinaTime:(NSDate *)date;
// 时间转时间戳
+ (NSString *)timeConvStamp:(NSString *)time;
+(NSString *)timeStampConv:(NSString *)timestamp{
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[timestamp integerValue]];
    //NSTimeZone *zone = [NSTimeZone systemTimeZone]; //时区偏移
    //NSInteger interval = [zone secondsFromGMTForDate:confromTimesp];
    //NSDate *localeDate = [confromTimesp  dateByAddingTimeInterval: interval];
    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [dateFormat stringFromDate:confromTimesp];
}
+ (NSString *)timeSecConv:(NSString *)second{
    NSInteger hours=[second integerValue]/3600;
    NSInteger minutes=([second integerValue]-hours*3600)/60;
  
    NSInteger seconds=[second integerValue]-hours*3600-minutes*60;
    return [NSString stringWithFormat:@"%02ld:%02ld:%02ld",(long)hours,(long)minutes,(long)seconds];
}
+ (NSDate *)stringToDate:(NSString *)dateString withDateFormat:(NSString *)format
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    
    NSDate *date = [dateFormatter dateFromString:dateString];
    return date; //[JZanFunc worldTimeToChinaTime:date];
}
+ (NSDate *)worldTimeToChinaTime:(NSDate *)date
{
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    NSInteger interval = [timeZone secondsFromGMTForDate:date];
    NSDate *localeDate = [date  dateByAddingTimeInterval:interval];
    return localeDate;
}
+ (NSString *)timeConvStamp:(NSString *)time{
    NSDate* date = [JZanFunc stringToDate:time withDateFormat:@"yyyy-MM-dd HH:mm"];
    NSTimeInterval a=[date timeIntervalSince1970]; // *1000 是精确到毫秒,不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", a]; //转为字符型
    return timeString;
}

扫描二维码推送至手机访问。

版权声明:本文由小祥子的博客发布,如需转载请注明出处。

本文地址:http://www.xiaoxiangzi.com/post/48.html

相关文章

测评EVO无人机

在本地的无人机群里了解到EVO无人机,于是在淘宝里买了一台回来玩,但收到货后发现跟想象的不一样。关于APP与遥控器首先在APPStore中没有找到这款无人机的APP,国内各大安卓市场也没有找到,最后在...

一键生成网站程序生成了个防水材料网

用一键生成网站的程序做了个防水材料网站,也顺便在这里吸引下蜘蛛 www.fangshui123.com 。弄了定时发布,以及伪静态,一键只是将相关的关键词入库,再有ASP文件来采集相应关键词...

《网络通信安全管理员》中级考试题目

  上周六与周日培训了两天,然后紧跟着周一就考试,通信管理局办的考试,内容是《网络通信安全管理员》,公司办经营性ICP备案必须得有一个中级和一个初级。我考的中级,得半个月后才有结果,不过我觉得应该可以...

新上线一款工具——google搜索

新上线一款工具——google搜索

 由于Google被国内封了,所以做了个google搜索结果调用的程序。 地址:http://www.xiaoxiangzi.com/tool/google.h...

最近做的几款在线工具

 由于做网站经常会用到一些在线工具,所以干脆集成到自己的网站“小祥子”里。HTML/JS转换工具 (前端常用)JS/HTML格式化工具 (前端常用,实际上dw上有HTML...

AJAX无刷新彩票开奖程序制作

AJAX无刷新彩票开奖程序制作

 对彩票开奖进行显示的一个程序,采用ASP+ACCESS+AJAX+XML技术制作而成。Ajax调用数据,后台彩票开奖记录后前台无刷新显示;用户可自助生成开奖调用界面,可供其他网站对开奖数据...