NSDateFormatter 使用方法

首先获取系统当前时间:

NSCalendar *cal = [NSCalendarcurrentCalendar];

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

NSDateComponents *dd = [calcomponents: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];

=================================================================================================

NSDateFormatter使用示例:

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

[dateFormatter setAMSymbol:@"AM"];

[dateFormatter setPMSymbol:@"PM"];

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

NSDate *date = [NSDate date];

NSString * s = [dateFormatterstringFromDate:date];

显示效果为15/08/201116:20PM

=================================================================================================

来源于Cocoachina

附上一些常用的格式:

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

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

h:mm a                    12:08PM

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

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

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



=================================================================================================

原文:http://blog.csdn.net/shanlei2011/article/details/6661488

得到大写的英文星期数:

       使用NSDateFormatter转换日期时,得到的英文字母的星期几只能是这样, 如Sun, Mon, etc.

    如果想得到大写字母的星期几,可以这样:

    NSArray *weekdayAry = [NSArray arrayWithObjects:@"SUN", @"MON", @"TUE", @"WED", @"THU", @"FRI",         @"SAT", nil];

    
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:NSLocalizedString(@"YYYY.MM.dd.eee", nil)];
    [dateFormatter setShortWeekdaySymbols:weekdayAry];
    [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
...
...

    // 此处更改显示的大写字母的星期几

    NString *str = [dateFormatter stringFromDate:[NSDate date]];


=================================================================================================

unicode.org/中对Date_Format_Patterns的说明:

Date Field Symbol Table
FieldSym.No.ExampleDescription
eraG1..3ADEra - 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.
4Anno Domini
5A
yeary1..n1996Year. Normally the length specifies the padding, but for two letters it also specifies the maximum length. Example:
Yearyyyyyyyyyyyyyyy
AD 1101001000100001
AD 121212012001200012
AD 12312323123012300123
AD 12341234341234123401234
AD 123451234545123451234512345
Y1..n1997Year (of "Week of Year"), used in ISO year-week calendar. May differ from calendar year.
u1..n4601Extended 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.
quarterQ1..202Quarter - Use one or two for the numerical quarter, three for the abbreviation, or four for the full name.
3Q2
42nd quarter
q1..202Stand-Alone Quarter - Use one or two for the numerical quarter, three for the abbreviation, or four for the full name.
3Q2
42nd quarter
monthM1..209Month - Use one or two for the numerical month, three for the abbreviation, or four for the full name, or five for the narrow name.
3Sept
4September
5S
L1..209Stand-Alone Month - Use one or two for the numerical month, three for the abbreviation, or four for the full name, or 5 for the narrow name.
3Sept
4September
5S
weekw1..227Week of Year.
W13Week of Month
dayd1..21Date - Day of the month
D1..3345Day of year
F12
 
Day of Week in Month. The example is for the 2nd Wed in July
g1..n2451334Modified 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
E1..3TuesDay of week - Use one through three letters for the short day, or four for the full name, or five for the narrow name.
4Tuesday
5T
e1..22Local 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.
3Tues
4Tuesday
5T
c12Stand-Alone local day of week - Use one letter for the local numeric value (same as 'e'), three for the short day, or four for the full name, or five for the narrow name.
3Tues
4Tuesday
5T
perioda1AMAM or PM
hourh1..211Hour [1-12].
H1..213Hour [0-23].
K1..20Hour [0-11].
k1..224Hour [1-24].
minutem1..259Minute. Use one or two for zero padding.
seconds1..212Second. Use one or two for zero padding.
S1..n3457Fractional Second - rounds to the count of letters. (example is for 12.34567)
A1..n69540000Milliseconds in day. This field behaves exactly like a composite of all time-related fields, not including the zone fields. As such, it also reflects discontinuities of those fields on DST transition days. On a day of DST onset, it will jump forward. On a day of DST cessation, it will jump backward. This reflects the fact that is must be combined with the offset field to obtain a unique local time value.
zonez1..3PDTTimezone - Use one to three letters for the short timezone or four for the full name. For more information, seeAppendix J: Time Zone Display Names
4Pacific Daylight Time
Z1..3-0800Use one to three letters for RFC 822, four letters for GMT format.
4GMT-08:00
v1PTUse one letter for short wall (generic) time, four for long wall time. For more information, seeAppendix J: Time Zone Display Names
4Pacific 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.




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值