zoukankan      html  css  js  c++  java
  • NSdate 时间格式

    NSdate 时间格式

    NSTimeInterval 时间间隔 基本单位 秒

    NSDateFormatter 时间格式器 用于日期对象的格式化或字符串解析为日期对象

    日期格式如下:

    y  年

    M  年中的月份

    D  当天是今年的第多少天

    d  月份中的天数

    F  月份中的周数

    E  星期几

    a  Am/pm

    H  一天中的小时数(0-23)

    k  一天中的小时数(1-24)

    K  am/pm 中的小时数(0-11)  Number  0

    h  am/pm 中的小时数(1-12)  Number  12

    m  小时中的分钟数  Number  30

    s  分钟中的秒数  Number  55

    S  毫秒数  Number  978

    z  时区  General time zone  Pacific Standard Time; PST; GMT-08:00

    Z  时区  RFC 822 time zone  -0800

    时间戳

    时间格式 Nsdate

    比较时间

    1 比较两个日期是不是同一日期 isEqualToDate;

    (2) 获取比较早的时间 earlierDate

    3 获取比较晚的时间 laterDate

    //初始化一个时间格式

    NSDate *curDate =[NSDate date];

    把时间格式转化成字符串样式 (但输出的时间比本地时间少8个小时(因为输出的是国际标准时间))

        NSLog(@"%@",[NSString stringWithFormat:@“%@“,curDate]);

    用NSDateFormatter(时间格式器)转换的时间 是转换成设备时间

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

        formatter.dateFormat =@"a h:m:s”;

     NSString *times = [formatter stringFromDate:dates];

        NSLog(@“%@",times);

    如以上代码,如果在下午3点半输出的则输出为:下午 3:30:43(精确到秒)

     NSTimeInterval timeinterval =[dates timeIntervalSince1970];

        NSString *timestring =[NSString stringWithFormat:@"%d",(int)timeinterval];

        NSLog(@“时间戳:%@",timestring);

    输出的时间戳是以秒为单位 相当于1970年到现在有多少秒的时间

        

    计算当前时间到时间间隔的日期

     获得一天的时间间隔

        NSTimeInterval interval = 24 *60*60;(以秒为单位)

       获得昨天的日期 当前的时间减去一天的时间(interval)

        NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow:-interval];(-为减 +为加)

       设置时间格式为 年月日:

        famtetter.dateFormat = @“y-M-d”;(自定义的时间输出格式)

    系统给的时间输出格式(共有5种样式)

    formatter.dateStyle =  NSDateFormatterFullStyle;

        NSLog(@"%@",[famtetter stringFromDate:yesterday]);

  • 相关阅读:
    appium连接真机时,报错:error: device unauthorized.
    python使用163邮箱发送测试报告遇到smtplib.SMTPAuthenticationError: (550, b'User has no permission')问题
    logging日志重复打印问题
    python实现text/html的get请求
    python实现Post请求四种请求体
    selenium异常类
    unittest所有断言方法
    windows下Jenkins+webdriver无法启动浏览器
    python3+selenium3之 解决:'chromedriver' executable needs to be in PATH问题
    python学习(6)--logging打印日志
  • 原文地址:https://www.cnblogs.com/popper123/p/4721617.html
Copyright © 2011-2022 走看看