3

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

Kalpesh
  • 5,336
  • 26
  • 41
  • I just ran it and got a different answer: `resultdate: 2013-01-01 00:00:00 +0000` – Robert Jul 03 '13 at 13:55
  • 1
    Perhaps `setWeekOfYear:`? (Just guessing) – David Rönnqvist Jul 03 '13 at 13:56
  • 1
    There is also this part of the documentation: "Using an NSCalendar with a calendar year and a weekday when using a calendar created using the week-based calendar constants results in ambiguous dates." It sounds like you may be hitting that one. – David Rönnqvist Jul 03 '13 at 13:57
  • 1
    Should you perhaps have `NSWeekCalendarUnit` as one of your `NSDateComponents`? – Robert Jul 03 '13 at 14:01

2 Answers2

6

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.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
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

Kalpesh
  • 5,336
  • 26
  • 41