zoukankan      html  css  js  c++  java
  • NSDateFormatter

    iOS时间那点事

    NSDateFormatter

    NSDateFormatter是NSFormatter的子类,另,NSFormatter的用途是“将数据在字符串与特定类型的对象之间转换”,目前NSFormatter只有两个子类NSNumberFormatter和NSDateFormatter。

    尽管NSDateFormatter提供了许多已定义好的时间格式,但是开发中开发人员更加喜欢自定义时间格式。

    1. 将时间字符串转换到NSDate对象,一般都是使用"年月日 时分秒”,数据库中的date类型基本上也是这样的时间类型。 格式一般为:yyyy-MM-dd HH:mm:ss。

      注意:yyyy是小写的;大写的YYYY的意思有些不同——“将这一年中第一周的周日当作今年的第一天”,因此有时结果和yyyy相同,有时就会不同。

    2. 将NSDate对象转换成特定格式的字符串。

      转换后的字符串会根据设备的“区域格式”,显示特定语言的结果。假如程序需要保证不同语言环境下显示一致,请注意这方面的问题,使用其他代替方法!

      一言足以明志:

      <!-- lang: cpp -->
      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
      [dateFormatter setDateFormat:@"'公元前/后:'G  '年份:'u'='yyyy'='yy '季度:'q'='qqq'='qqqq '月份:'M'='MMM'='MMMM '今天是今年第几周:'w '今天是本月第几周:'W  '今天是今天第几天:'D '今天是本月第几天:'d '星期:'c'='ccc'='cccc '上午/下午:'a '小时:'h'='H '分钟:'m '秒:'s '毫秒:'SSS  '这一天已过多少毫秒:'A  '时区名称:'zzzz'='vvvv '时区编号:'Z "];
      NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]);
      

      OutPut:

      1. 区域格式:美国

        公元前/后:AD  年份:2013=2013=13 季度:3=Q3=3rd quarter 月份:8=Aug=August 今天是今年第几周:32 今天是本月第几周:2  今天是今天第几天:219 今天是本月第几天:7 星期:4=Wed=Wednesday 上午/下午:AM 小时:1=1 分钟:24 秒:32 毫秒:463  这一天已过多少毫秒:5072463  时区名称:China Standard Time=China Standard Time 时区编号:+0800
        
      2. 区域格式:中国

        公元前/后:公元  年份:2013=2013=13 季度:3=三季度=第三季度 月份:8=8月=8月 今天是今年第几周:32 今天是本月第几周:2  今天是今天第几天:219 今天是本月第几天:7 星期:4=周三=星期三 上午/下午:上午 小时:1=1 分钟:44 秒:30 毫秒:360  这一天已过多少毫秒:6270360  时区名称:中国标准时间=中国标准时间 时区编号:+0800
        
    3. 过多使用NSDateFormatter将影响程序的性能,且程序中NSDateFormatter对象的时间格式基本一致,所以使用NSDateFormatter的时候尽量使用单例模式。

    !!!!奉上NSDateFormatter的时间格式: 更详细的官方说明

    a:  AM/PM
    A:  0~86399999 (Millisecond of Day)
    
    c/cc:   1~7 (Day of Week)
    ccc:    Sun/Mon/Tue/Wed/Thu/Fri/Sat
    cccc: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday
    
    d:  1~31 (0 padded Day of Month)
    D:  1~366 (0 padded Day of Year)
    
    e:  1~7 (0 padded Day of Week)
        E~EEE:  Sun/Mon/Tue/Wed/Thu/Fri/Sat
    EEEE: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday
    
    F:  1~5 (0 padded Week of Month, first day of week = Monday)
    
    g:  Julian Day Number (number of days since 4713 BC January 1)
        G~GGG:  BC/AD (Era Designator Abbreviated)
    GGGG:   Before Christ/Anno Domini
    
    h:  1~12 (0 padded Hour (12hr))
    H:  0~23 (0 padded Hour (24hr))
    
    k:  1~24 (0 padded Hour (24hr)
    K:  0~11 (0 padded Hour (12hr))
    
    L/LL:   1~12 (0 padded Month)
    LLL:    Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
    LLLL: January/February/March/April/May/June/July/August/September/October/November/December
    
    m:  0~59 (0 padded Minute)
    M/MM:   1~12 (0 padded Month)
    MMM:    Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
    MMMM: January/February/March/April/May/June/July/August/September/October/November/December
    
    q/qq:   1~4 (0 padded Quarter)
    qqq:    Q1/Q2/Q3/Q4
    qqqq:   1st quarter/2nd quarter/3rd quarter/4th quarter
    Q/QQ:   1~4 (0 padded Quarter)
    QQQ:    Q1/Q2/Q3/Q4
    QQQQ:   1st quarter/2nd quarter/3rd quarter/4th quarter
    
    s:  0~59 (0 padded Second)
    S:  (rounded Sub-Second)
    
    u:  (0 padded Year)
    
    v~vvv:  (General GMT Timezone Abbreviation)
    vvvv:   (General GMT Timezone Name)
    
    w:  1~53 (0 padded Week of Year, 1st day of week = Sunday, NB: 1st week of year starts from the last Sunday of last year)
    W:  1~5 (0 padded Week of Month, 1st day of week = Sunday)
    
    y/yyyy: (Full Year)
    yy/yyy: (2 Digits Year)
    Y/YYYY: (Full Year, starting from the Sunday of the 1st week of year)
    YY/YYY: (2 Digits Year, starting from the Sunday of the 1st week of year)
    
    z~zzz:  (Specific GMT Timezone Abbreviation)
    zzzz:   (Specific GMT Timezone Name)
    Z:  +0000 (RFC 822 Timezone)
  • 相关阅读:
    InfoQ访谈BPEL4People代表
    传 IBM 拟 4 月 6 日宣布收购 Sun
    NetBeans 6.7 Milestone 3 Now Available for Download!
    Intel比AMD高明在哪里?
    InfoQ访谈BPEL4People代表
    Linux 3.8.1 电源管理之OMAP Voltage Domain分析
    Readline简介 Linux技术问答 Linux中国 | Linux.cn 我们的Linux中文社区
    更改日期
    JAVA研发工程师(YF)
    一键解决Ubuntu下安装Eclipse Android/C/C++ 开发环境
  • 原文地址:https://www.cnblogs.com/luqinbin/p/4911208.html
Copyright © 2011-2022 走看看