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

OC时间戳转时间方法系列

8年前 (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

相关文章

一个人的旅行·丽江

一个人的旅行·丽江

 大理玩了三四天,下一站,丽江。茶马古道拉市海玉龙雪山玉龙雪山下的蓝月谷泸沽湖去泸沽湖的时候,可谓是一波三折。遇到坑爹的旅行社,我的费用比同车人最低的多一倍,比除我之外最高的也多100,最终...

开始学习IOS

   找了几百G的IOS视频(用不着看完),开始学习IOS(即使电脑还木有),准备学SWIFT,不打算学Objective-C,但已经下好的却是Objective-C的IOS教程,凑合看了一...

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

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

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

IOS开发环境配置——黑苹果安装

  最近准备做苹果手机应用开发,不过看了下得在Mac OS 10.10系统下才能做开发,所以有三个选择给我。  一、买台MacBook,我认为暂时没有买的必要,得花万把块钱~等我学会了再买  二、装个...

没有特长,凭什么拿高薪?

   今天跟大舅一起散步的时候,说到谁谁谁嫌工资低,大舅说“没有特长,怎么拿的到高工资?”。的确如此,以前做网站挣点钱,当然学这个的动力也是为了挣钱,网上接点小单,挣点小钱,发现多学一门技术...