简体   繁体   中英

How to Get date from week number and year

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..

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.

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

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