简体   繁体   中英

iphone: find date for 4th day of week

I used NSDateComponents to find date for 4th, 2nd day of the week like this

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit |
                                                                 NSMonthCalendarUnit |
                                                                 NSYearCalendarUnit |
                                                                 NSWeekCalendarUnit | 
                                                                 NSWeekdayCalendarUnit)
                                                       fromDate:today];
[weekdayComponents setWeekday:4];


NSLog(@"Date at that day was %@",[gregorian dateFromComponents:weekdayComponents]);

but every time the output is

Date at that day was 2011-06-20 18:30:00 +0000

Can you help in this
Thanks in advance

[weekdayComponents setDay:weekdayComponents.day+ (4-weekdayComponents.weekday)];

至于NSlog输出:NSLog(@“%@”,somedate)以GMT0显示时间,但NSDateComponents的操作考虑了时区,因此2011-06-20 18:30:00 +0000开始于21.06 .2011在你的时区

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM