NSDateFormatter 日期格式,nsdateformatter

來源:互聯網
上載者:User

NSDateFormatter 日期格式,nsdateformatter

紀元的顯示:
G:顯示AD,也就是公元
 
年的顯示:
yy:年的後面2位數字
yyyy:顯示完整的年
 
月的顯示:
M:顯示成1~12,1位數或2位數
MM:顯示成01~12,不足2位數會補0
MMM:英文月份的縮寫,例如:Jan
MMMM:英文月份完整顯示,例如:January


日的顯示:
d:顯示成1~31,1位數或2位數
dd:顯示成01~31,不足2位數會補0
 
星期的顯示:
EEE:星期的英文縮寫,如Sun
EEEE:星期的英文完整顯示,如,Sunday

 
上/下午的顯示:
aa:顯示AM或PM


小時的顯示:
H:顯示成0~23,1位數或2位數(24小時制)
HH:顯示成00~23,不足2位數會補0(24小時制)
K:顯示成0~12,1位數或2位數(12小時制)
KK:顯示成0~12,不足2位數會補0(12小時制)


分的顯示:
m:顯示0~59,1位數或2位數
mm:顯示00~59,不足2位數會補0


秒的顯示:
s:顯示0~59,1位數或2位數
ss:顯示00~59,不足2位數會補0
S: 毫秒的顯示

Stackooverflow 上的一個回答:

/*    x           number    xx          two digit number    xxx         abbreviated name    xxxx        full name    a           AM/PM    A           millisecond of day    c           day of week (c,cc,ccc,cccc)    d           day of month    e           day of week (e,EEE,EEEE)    F           week of month    g           julian day (since 1/1/4713 BC)    G           era designator (G=GGG,GGGG)    h           hour (1-12, zero padded)    H           hour (0-23, zero padded)    L           month of year (L,LL,LLL,LLLL)    m           minute of hour (0-59, zero padded)    M           month of year (M,MM,MMM,MMMM)    Q           quarter of year (Q,QQ,QQQ,QQQQ)    s           seconds of minute (0-59, zero padded)    S           fraction of second    u           zero padded year    v           general timezone (v=vvv,vvvv)    w           week of year (0-53, zero padded)    y           year (y,yy,yyyy)    z           specific timezone (z=zzz,zzzz)    Z           timezone offset +0000    sql         y-M-d H:m:s    rss         [E, ]d MMM y[y] H:m:s Z|z[zzz]*/

This is my comment for date parsing. I use the following, where toDateUsingFormat uses an NSDateFormatter with the passed in string. I do not use a locale, because rss dates are not localized.

    if ( 0 == [string rangeOfString:@","].length ) {        result = [string toDateUsingFormat:@"d MMM y H:m:s z"];    } else {        result = [string toDateUsingFormat:@"E, d MMM y H:m:s z"];    }

Edit:

I use getObjectValue: instead of dateFromString.

NSDate *result = nil;NSError *error = nil;[dataFormatter getObjectValue:&result forString:dateString errorDescription:&error];

tips:

1、設定時間格式為 “MMM dd,yyyy” ,真機調試的時候會出現 “Mar. 24,2015”這樣的情況(模擬器不會出現),多出了一個點,加上這一句就好了。

dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.