設定日期輸出樣式

設定日期輸出樣式

要設定日期輸出樣式就要交給NSDateFormattersetDateStyle 來處理,本身它提供了五種格式

NSDateFormatterNoStyle
NSDateFormatterShortStyle
NSDateFormatterMediumStyle
NSDateFormatterLongStyle
NSDateFormatterFullStyle

接下來就要根據五種格式做個顯示的示範

//-----------start-----------
    NSDate *today = [NSDate dateWithTimeIntervalSinceNow:0];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateStyle:NSDateFormatterNoStyle];
    NSLog(@"NSDateFormatterNoStyle: %@", [dateFormat stringFromDate:today]);
    //
    [dateFormat setDateStyle:NSDateFormatterShortStyle];
    NSLog(@"NSDateFormatterShortStyle: %@", [dateFormat stringFromDate:today]);
    //
    [dateFormat setDateStyle:NSDateFormatterMediumStyle];
    NSLog(@"NSDateFormatterMediumStyle: %@", [dateFormat stringFromDate:today]);
    //
    [dateFormat setDateStyle:NSDateFormatterLongStyle];
    NSLog(@"NSDateFormatterLongStyle: %@", [dateFormat stringFromDate:today]);
    //
    [dateFormat setDateStyle:NSDateFormatterFullStyle];
    NSLog(@"NSDateFormatterFullStyle: %@", [dateFormat stringFromDate:today]);
    //

//------------end------------

輸出結果:

//-----------start-----------
2013-03-28 19:25:18.257 testa1[29866:c07] NSDateFormatterNoStyle:
2013-03-28 19:25:18.258 testa1[29866:c07] NSDateFormatterShortStyle: 3/28/13
2013-03-28 19:25:18.259 testa1[29866:c07] NSDateFormatterMediumStyle: Mar 28, 2013
2013-03-28 19:25:18.260 testa1[29866:c07] NSDateFormatterLongStyle: March 28, 2013
2013-03-28 19:25:18.260 testa1[29866:c07] NSDateFormatterFullStyle: Thursday, March 28, 2013
//------------end------------