简体   繁体   English

在IO中获取当前星期几的日期

[英]Get the date of sunday of the current week in IOs

I have searched for the solution to find the date of the sunday of the particular week.I have found questions that have been answered but it doesnt show the correct date of sunday. 我一直在寻找解决方案以查找特定星期几的日期。我已经找到了可以回答的问题,但没有显示正确的星期几。

NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekday | NSCalendarUnitWeekOfYear;
NSDateComponents *comps = [calendar components:unitFlags fromDate:[NSDate date]];
comps.weekday = 1;  //Sunday
comps.weekOfMonth = 1;
NSDate *sundayDate = [calendar dateFromComponents:comps];

When i print out the date it gives this. 当我打印出日期时,它给出了这一点。

2015-07-08 10:14:00 +0000

Its the current date.What am i doing wrong? 它是当前日期。我在做什么错?

hope this helps you : 希望这对您有帮助:

    NSDate *today = [NSDate date];
    NSCalendar *calenderObj = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    calenderObj.firstWeekday = 1; // Sunday = 1, 
     // NSDateComponents *components = [calenderObj components:NSWeekOfYearCalendarUnit fromDate:today];
    NSDate *objDate = nil;
 [calenderObj rangeOfUnit:NSWeekCalendarUnit startDate:&objDate interval:NULL forDate:today];
    NSLog(@" === %@",  objDate);

Output : 输出:

=== 2015-07-15 10:26:00 +0000 === 2015-07-15 10:26:00 +0000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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