作者zonble (zonble)
看板MacDev
标题Re: [问题] 两个问题
时间Thu Aug 5 23:59:26 2010
※ 引述《benjenq (Ben(憋))》之铭言:
: ※ 引述《amos6064 (amos)》之铭言:
: : 第一题我目前的作法希望对你有帮助
: : NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
: : [dateFormatter setDateFormat:@"yyyy"];
: 不过iOS4加入了[民国历]这东西,
: 这行回传的内容就变成了0099...得到手机设定改西元历才行。
顺道一提,如果是单纯要拿日期里头的数字,用 NSCalendar 与
NSDateComponents 写,写出来的程式会比较好懂,在 NSCalendar
这边也可以指定要使用哪一种月历,比方说,如果我指定的是
民国历:
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSRepublicOfChinaCalendar] autorelease];
NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]];
NSLog(@"Year:%d", [components year]);
NSLog(@"Month:%d", [components month]);
NSLog(@"Day:%d", [components day]);
输出的结果就是
2010-08-05 23:56:13.773 Date[778:207] Year:99
2010-08-05 23:56:13.774 Date[778:207] Month:8
2010-08-05 23:56:13.775 Date[778:207] Day:5
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.42.198.122
1F:推 benjenq:的确啊~容易理解得多了~且比较不会出错 08/07 14:18