IOS:NSDateComponent 的问题
我有这个代码:
- (void) setDataLabel{
for (int k = 0; k<31; k++){
[[lineSunday objectAtIndex:k] setAlpha:0.00];
[[arrayDay objectAtIndex:k] setTextColor:[UIColor whiteColor]];
}
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setYear:2011];
[components setDay:1];
[components setMonth:10];
//NSLog(@"mese:%d", month);
NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *firstDate = [gregorianCalendar dateFromComponents:components];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];
for (int i = 0; i < 31; i++) {
NSTimeInterval seconds = 24*60*60 * i;
NSDate *date = [NSDate dateWithTimeInterval:seconds sinceDate:firstDate];
NSDateComponents *weekdayComponents = [gregorianCalendar components:NSWeekdayCalendarUnit fromDate:date];
int weekday = [weekdayComponents weekday];
NSString *strDate = [dateFormatter stringFromDate: date];
[[arrayDay objectAtIndex:i] setText:strDate];
if (weekday == 1) {
[[arrayDay objectAtIndex:i] setTextColor:[UIColor redColor]];
[[lineSunday objectAtIndex:i] setAlpha:1.00];
}
}
这个代码设置了 31 个带有月份日期的标签,一切都很好,但我不明白为什么十月份有连续两个工作日;举个例子:在今年,这段代码在月末一天这样写:
....25 26 27 28 29 30 30
和 30 和 30 是红色,但不应该是这样,应该是这样是
....25 26 27 28 29 30 31
并且只有 30 必须是红色
为什么会发生这种情况?
I have this code:
- (void) setDataLabel{
for (int k = 0; k<31; k++){
[[lineSunday objectAtIndex:k] setAlpha:0.00];
[[arrayDay objectAtIndex:k] setTextColor:[UIColor whiteColor]];
}
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setYear:2011];
[components setDay:1];
[components setMonth:10];
//NSLog(@"mese:%d", month);
NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *firstDate = [gregorianCalendar dateFromComponents:components];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];
for (int i = 0; i < 31; i++) {
NSTimeInterval seconds = 24*60*60 * i;
NSDate *date = [NSDate dateWithTimeInterval:seconds sinceDate:firstDate];
NSDateComponents *weekdayComponents = [gregorianCalendar components:NSWeekdayCalendarUnit fromDate:date];
int weekday = [weekdayComponents weekday];
NSString *strDate = [dateFormatter stringFromDate: date];
[[arrayDay objectAtIndex:i] setText:strDate];
if (weekday == 1) {
[[arrayDay objectAtIndex:i] setTextColor:[UIColor redColor]];
[[lineSunday objectAtIndex:i] setAlpha:1.00];
}
}
This code set 31 labels with days of the month and it's all ok, but I don't understand why for October month there is ever two weekday consecutive; an example: in this year this code write at the end of the month the day in this way:
....25 26 27 28 29 30 30
and 30 and 30 are red colour, but it shouldn't be so, it should be
....25 26 27 28 29 30 31
and only 30 must be redcolour
Why it happen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为夏令时。我们在该循环中每天添加 86400 秒,但一天将有 25 个小时。
编辑:
最好的方法可能是只在循环中获取日期对象,而根本不进行花哨的计算。
It's because of daylight saving time. We are adding 86400 seconds for each day in that loop, but one day will have 25 hours.
Edit:
Best approach is probably to just get the date object in the loop as well and not doing fancy calculations at all.