例如:
//实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//设定时间格式,这里可以设置成自己需要的格式
//精确到毫秒
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSSS"];
//用[NSDate date]可以获取系统当前时间
NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
//输出格式为:2010-10-27 10:22:13.0000
NSLog(@”%@”,currentDateStr);
//alloc后对不使用的对象别忘了release
[dateFormatter release];
----------------------------------------------------格式模型--------------------------------------------------
dd/MM/yyyy hh:mmaaa
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier
[formatter
yyyy 4位年 MM 月
d~dd 日 hh 时 HH 24小时 mm 分 ss 秒 a~aaa AM/PM
EEE 英文星期 MMM 英文月 Z~ZZZ 时区
ZZZZ K G~GGG AD
http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
PS: Jackie214大虾说,这是unicode标准哈. 其他程序的Date格式也是这么干的, 那么有兴趣的英文又好的TX就可以去:http://unicode.org/reports/tr35/#Date_Format_Patterns 逛逛啦..
继续我的文章!
IPhone开发中, OBjective-C中的NSDate是一个挺讨厌的类型, 自己不到转换成字符串的类型,还得带一个NSDateFormatter的类型.
官方文档上对NSDateFormatter的格式串好像没详讲, 或许有,我没找到, 每次使用都是用谷歌摸索.
有幸找到一份比较全的文档, 翻译过来共享:
a: AM/PM (上午/下午)
A: 0~86399999 (一天的第A微秒)
c/cc: 1~7 (一周的第一天, 周天为1)
ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat (星期几简写)
cccc: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday (星期几全拼)
d: 1~31 (月份的第几天, 带0)
D: 1~366 (年份的第几天,带0)
e: 1~7 (一周的第几天, 带0)
E~EEE: Sun/Mon/Tue/Wed/Thu/Fri/Sat (星期几简写)
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))
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)
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
Field | Sym. | No. | Example | Description | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
era | G | 1..3 | AD | Era - Replaced with the Era string for the current date. One to three letters for the abbreviated form, four letters for the long form, five for the narrow form. | ||||||||||||||||||||||||||||||||||||
4 | Anno Domini | |||||||||||||||||||||||||||||||||||||||
5 | A | |||||||||||||||||||||||||||||||||||||||
year | y | 1..n | 1996 | Year. Normally the length specifies the padding, but for two letters it also specifies the maximum length. Example:
| ||||||||||||||||||||||||||||||||||||
Y | 1..n | 1997 | Year (of "Week of Year"), used in ISO year-week calendar. May differ from calendar year. | |||||||||||||||||||||||||||||||||||||
u | 1..n | 4601 | Extended year. This is a single number designating the year of this calendar system, encompassing all supra-year fields. For example, for the Julian calendar system, year numbers are positive, with an era of BCE or CE. An extended year value for the Julian calendar system assigns positive values to CE years and negative values to BCE years, with 1 BCE being year 0. | |||||||||||||||||||||||||||||||||||||
quarter | Q | 1..2 | 02 | Quarter - Use one or two for the numerical quarter, three for the abbreviation, or four for the full name. | ||||||||||||||||||||||||||||||||||||
3 | Q2 | |||||||||||||||||||||||||||||||||||||||
4 | 2nd quarter | |||||||||||||||||||||||||||||||||||||||
q | 1..2 | 02 | Stand-Alone | |||||||||||||||||||||||||||||||||||||
3 | Q2 | |||||||||||||||||||||||||||||||||||||||
4 | 2nd quarter | |||||||||||||||||||||||||||||||||||||||
month | M | 1..2 | 09 | Month - Use one or two for the numerical month, three for the abbreviation, or four for the full name, or five for the narrow name. | ||||||||||||||||||||||||||||||||||||
3 | Sept | |||||||||||||||||||||||||||||||||||||||
4 | September | |||||||||||||||||||||||||||||||||||||||
5 | S | |||||||||||||||||||||||||||||||||||||||
L | 1..2 | 09 | Stand-Alone | |||||||||||||||||||||||||||||||||||||
3 | Sept | |||||||||||||||||||||||||||||||||||||||
4 | September | |||||||||||||||||||||||||||||||||||||||
5 | S | |||||||||||||||||||||||||||||||||||||||
week | w | 1..2 | 27 | Week of Year. | ||||||||||||||||||||||||||||||||||||
W | 1 | 3 | Week of Month | |||||||||||||||||||||||||||||||||||||
day | d | 1..2 | 1 | Date - Day of the month | ||||||||||||||||||||||||||||||||||||
D | 1..3 | 345 | Day of year | |||||||||||||||||||||||||||||||||||||
F | 1 | 2 | Day of Week in Month. The example is for the 2nd Wed in July | |||||||||||||||||||||||||||||||||||||
g | 1..n | 2451334 | Modified Julian day. This is different from the conventional Julian day number in two regards. First, it demarcates days at local zone midnight, rather than noon GMT. Second, it is a local number; that is, it depends on the local time zone. It can be thought of as a single number that encompasses all the date-related fields. | |||||||||||||||||||||||||||||||||||||
week day | E | 1..3 | Tues | Day of week - Use one through three letters for the short day, or four for the full name, or five for the narrow name. | ||||||||||||||||||||||||||||||||||||
4 | Tuesday | |||||||||||||||||||||||||||||||||||||||
5 | T | |||||||||||||||||||||||||||||||||||||||
e | 1..2 | 2 | Local day of week. Same as E except adds a numeric value that will depend on the local starting day of the week, using one or two letters. For this example, Monday is the first day of the week. | |||||||||||||||||||||||||||||||||||||
3 | Tues | |||||||||||||||||||||||||||||||||||||||
4 | Tuesday | |||||||||||||||||||||||||||||||||||||||
5 | T | |||||||||||||||||||||||||||||||||||||||
c | 1 | 2 | Stand-Alone | |||||||||||||||||||||||||||||||||||||
3 | Tues | |||||||||||||||||||||||||||||||||||||||
4 | Tuesday | |||||||||||||||||||||||||||||||||||||||
5 | T | |||||||||||||||||||||||||||||||||||||||
period | a | 1 | AM | AM or PM | ||||||||||||||||||||||||||||||||||||
hour | h | 1..2 | 11 | Hour [1-12]. | ||||||||||||||||||||||||||||||||||||
H | 1..2 | 13 | Hour [0-23]. | |||||||||||||||||||||||||||||||||||||
K | 1..2 | 0 | Hour [0-11]. | |||||||||||||||||||||||||||||||||||||
k | 1..2 | 24 | Hour [1-24]. | |||||||||||||||||||||||||||||||||||||
minute | m | 1..2 | 59 | Minute. Use one or two for zero padding. | ||||||||||||||||||||||||||||||||||||
second | s | 1..2 | 12 | Second. Use one or two for zero padding. | ||||||||||||||||||||||||||||||||||||
S | 1..n | 3457 | Fractional Second - rounds to the count of letters. (example is for 12.34567) | |||||||||||||||||||||||||||||||||||||
A | 1..n | 69540000 | Milliseconds in day. This field behaves | |||||||||||||||||||||||||||||||||||||
zone | z | 1..3 | PDT | Timezone - Use one to three letters for the short timezone or four for the full name. For more information, see | ||||||||||||||||||||||||||||||||||||
4 | Pacific Daylight Time | |||||||||||||||||||||||||||||||||||||||
Z | 1..3 | -0800 | Use one to three letters for RFC 822, four letters for GMT format. | |||||||||||||||||||||||||||||||||||||
4 | GMT-08:00 | |||||||||||||||||||||||||||||||||||||||
v | 1 | PT | Use one letter for short wall (generic) time, four for long wall time. For more information, see | |||||||||||||||||||||||||||||||||||||
4 | Pacific Time |
All non-letter character represent themselves in a pattern, except for the single quote. It is used to 'escape' letters. Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote.