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

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

相关文章

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

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

想想今天做了些啥,有总结才有进步

  让我好好的回忆下今天...  今天我依然在上班,昨天一个朋友问我愿不愿意放弃现在的工作,一起去拍微电影,因为钱包太小风险太大,所以么打算去。当然还有其他原因,我就不说了。  坐在我左边的同事走了,...

一个程序员如何快速赚到一百万

          一个程序员如何快速赚到一百万,说的详细点儿就是:一个固定工作者怎么跳出固有的模式...

滇池红嘴鸥

滇池红嘴鸥

 拍摄时间:2014年11月8日拍摄地点:昆明·海埂大坝使用器材:佳能70D + 55-250mm...

人像摄影

人像摄影

12.25地点:翠湖时间:2014年12月25日  地点:云大时间:2014年12月28日...