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]);

  • 相关阅读:
    C语言堆栈入门——堆和栈的区别
    Unity时钟定时器插件——Vision Timer源码分析之一
    UNITY3D 2D物流流体插件下载|Liquid Physics 2D
    Unity3d插件Master Audio AAA Sound v3.5
    游戏行业的女性拥有强大的新盟友:Facebook
    Unity游戏设计与实现 南梦宫一线程序员的开发实例
    Unity4.6证书激活问题
    里诺全系列注册机+暗桩patch
    冰点还原8.53破解版
    NSE: known a priori estimate
  • 原文地址:https://www.cnblogs.com/popper123/p/4721617.html
Copyright © 2011-2022 走看看