How to Get date from week number and year

时光总嘲笑我的痴心妄想 提交于 2019-12-14 01:48:00

问题


I want to get date from week number and year . i got week number and year from server . I am trying following code but it wont work.

 NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter2 setDateFormat:@"yyyy-MM-dd"];

NSDate *now = [dateFormatter2 dateFromString:@"2001-01-01"];
NSCalendar *gregorian1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps1 = [gregorian1 components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:now];


[comps1 setYear:2013];
[comps1 setWeek:51];
[comps1 setWeekday:2];

NSDate *resultDate = [gregorian1 dateFromComponents:comps1];

Output is :- 2012-12-31 18:30:00 +0000

what am i doing wrong ? Thanks in advance..


回答1:


Your problem is that you have ambiguous components in your NSDateComponents object, because you created the components from an existing NSDate. Put a breakpoint in your code and look at comps1:

(lldb) po comps1
$0 = 0x0ab354f0 <NSDateComponents: 0xab354f0>
    Calendar Year: 2013
    Month: 1
    Leap month: no
    Day: 1
    Week (obsolescent): 51
    Weekday: 2

It's kind of hard to create a date on monday in week 51 that is January, 1st at the same time.

When you want to create a NSDate with NSDateComponents you should start with "fresh" components. Like this:

NSCalendar *gregorian1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps1 = [[NSDateComponents alloc] init];

[comps1 setYear:2013];
[comps1 setWeek:51];
[comps1 setWeekday:2];

NSDate *resultDate = [gregorian1 dateFromComponents:comps1];

Result: 2013-12-16 (which is hopefully what you expected)

The code that alloc inits the components has these components:

(lldb) po comps1
$0 = 0x0a37d220 <NSDateComponents: 0xa37d220>
    Calendar Year: 2013
    Leap month: no
    Week (obsolescent): 51
    Weekday: 2

No ambiguity.

Additionally it would be a good idea to replace [comps1 setWeek:51]; with [comps1 setWeekOfYear:51]; But your main problem was the reuse of an existing NSDateComponents object.




回答2:


I got solution

  NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter2 setDateFormat:@"yyyy-MM-dd"];

NSDate *now = [dateFormatter2 dateFromString:@"2001-01-01"];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian.firstWeekday = 2; // Sunday = 1, Saturday = 7

NSDateComponents *components = [gregorian components:NSYearCalendarUnit |NSWeekdayCalendarUnit fromDate:now];
[components setYear:2013];
[components setWeekOfYear:18];
[components setWeekday:2];

NSDate *resultDate = [gregorian dateFromComponents:components];

Output is :- 2013-04-28 18:30:00 +0000



来源:https://stackoverflow.com/questions/17449810/how-to-get-date-from-week-number-and-year

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!