zoukankan      html  css  js  c++  java
  • iOS中时间与时间戳的相互转化

    iOS中时间与时间戳的相互转化

    //获取当前系统时间的时间戳

    #pragma mark - 获取当前时间的 时间戳

    +(NSInteger)getNowTimestamp{

        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/Beijing"];

        [formatter setTimeZone:timeZone];

        NSDate *datenow = [NSDate date];//现在时间

        

        NSLog(@"设备当前的时间:%@",[formatter stringFromDate:datenow]);

        //时间转时间戳的方法:

       

        NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];

        

        NSLog(@"设备当前的时间戳:%ld",(long)timeSp); //时间戳的值

        

        return timeSp;

    }

    //将某个时间转化成 时间戳

    #pragma mark - 将某个时间转化成 时间戳

    +(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{

        

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

        [formatter setDateStyle:NSDateFormatterMediumStyle];

        [formatter setTimeStyle:NSDateFormatterShortStyle];

        [formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制

        

        NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];

        [formatter setTimeZone:timeZone];

        

        NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate

        //时间转时间戳的方法:

        NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];

        

        NSLog(@"将某个时间转化成 时间戳&&&&&&&timeSp:%ld",(long)timeSp); //时间戳的值

        

        return timeSp;

    }

    //将某个时间戳转化成 时间

    #pragma mark - 将某个时间戳转化成 时间

    +(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{

        

        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

        [formatter setDateStyle:NSDateFormatterMediumStyle];

        [formatter setTimeStyle:NSDateFormatterShortStyle];

        [formatter setDateFormat:format]; // (@"YYYY-MM-dd hh:mm:ss")----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制

        NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];

        [formatter setTimeZone:timeZone];

        NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];

        NSLog(@"1296035591  = %@",confromTimesp);

        

        NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];

        

        //NSLog(@"&&&&&&&confromTimespStr = : %@",confromTimespStr);

        

        return confromTimespStr;

    }

    感谢您的访问! 若对您有帮助或有兴趣请关注博客:http://www.cnblogs.com/Rong-Shengcom/
  • 相关阅读:
    Nginx 使用 GeoIP 模块区分用户地区
    使用nginx转发tcp请求(解决访问内网的腾讯云redis)
    open file cache提升nginx性能
    使用 nginx-http-concat
    使用goaccess对Nginx日志简单分析
    Zookeeper系列一:Zookeeper基础命令操作
    k8s nginx应用-获取客户端访问真实IP
    mysql 备份数据库中的一张表
    ssh命令带密码
    Linux下grep显示前后几行信息
  • 原文地址:https://www.cnblogs.com/Rong-Shengcom/p/6080568.html
Copyright © 2011-2022 走看看