羅列NSDateFormatter各類格式參數

格式化日期字符串,copy一份經常使用參數:html

a: AM/PM (上午/下午)git

A: 0~86399999 (一天的第A微秒)spa

c/cc: 1~7 (一週的第一天, 周天爲1)component

ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat (星期幾簡寫)orm

cccc: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday (星期幾全拼)htm

d: 1~31 (月份的第幾天, 帶0)blog

D: 1~366 (年份的第幾天,帶0)ci

e: 1~7 (一週的第幾天, 帶0)字符串

E~EEE: Sun/Mon/Tue/Wed/Thu/Fri/Sat (星期幾簡寫)get

EEEE: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday (星期幾全拼)

F: 1~5 (每個月的第幾周, 一週的第一天爲週一)

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)) 帶0的時, 12小時制

H: 0~23 (0 padded Hour (24hr))  帶0的時, 24小時制

k: 1~24 (0 padded Hour (24hr) 帶0的時, 24小時制

K: 0~11 (0 padded Hour (12hr)) 帶0的時, 12小時制

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) 常規GMT時區的編寫

vvvv: (General GMT Timezone Name) 常規GMT時區的名稱

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)  2個數字的年份

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) 指定GMT時區的編寫

zzzz: (Specific GMT Timezone Name) Z: +0000 (RFC 822 Timezone) 指定GMT時區的名稱

 

實例:

http://blog.sina.com.cn/s/blog_60b45f230100i0r9.html

NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init]autorelease];

[dateFormatter setAMSymbol:@"AM"];

[dateFormatter setPMSymbol:@"PM"];

[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mmaaa"];

NSDate *date = [NSDate date];

NSString * s = [dateFormatter stringFromDate:date];

顯示效果爲:10/05/2010 03:49PM

 

 

下面附上幾個格式:這個是從Cocoachina上來的。呵呵。

yyyy:MM:dd G 'at' HH:mm:ss zzz            1996.07.10 AD at 15:08:56 PDT

EEE, MMM d, "yy                                         Wed,july 10, '99

h:mm a                                                     12:08 PM

hh 'o"clock' a,zzzz                                      12 o'clock PM, Pacific Daylight Time

K:mm a, z                                                 0:00 PM, PST

yyyyy,MMMM.dd GGG hh:mm aaa                01996.july.10 AD 12:08 PM

 

 

 

下面是獲得當前的年,月,日,時,分,秒。

NSCalendar *cal = [NSCalendar currentCalendar];

unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

NSDateComponents *dd = [cal components:unitFlags fromDate:date]; 

int y = [dd year];

int m = [dd month];

int d = [dd day];

int h = [dd hour];

int m = [dd minute];

int s = [dd second];

相關文章
相關標籤/搜索