3

In my application I'm using following codes to retrieve current date and day:

NSDate *today1 = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy :EEEE"];
    NSString *dateString11 = [dateFormat stringFromDate:today1];

    NSLog(@"date: %@", dateString11);
    //[dateFormat release];
    NSCalendar *gregorian11 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];  
    NSDateComponents *components1 = [gregorian11 components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today1];
    [components1 setDay:([components1 day]-([components1 weekday]-1))];

    NSDate *beginningOfWeek1 = [gregorian11 dateFromComponents:components1];
    NSDateFormatter *dateFormat_first = [[NSDateFormatter alloc] init];
    [dateFormat_first setDateFormat:@"dd/MM/yyyy :EEEE"];
    NSString *dateString_first = [dateFormat_first stringFromDate:beginningOfWeek1];

    NSLog(@"First_date: %@", dateString_first);

but using above code I only got the current day and Date and first day of week but I need to get last day of week.

Where I'm wrong in my code and what modification is needed to get last day/date of week?

halfer
  • 19,824
  • 17
  • 99
  • 186
nivrutti
  • 297
  • 4
  • 14

5 Answers5

3

For me this works:

Calculate first day in week

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents     = [gregorian components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];

Calcuate number of days to substract from today, in order to get the first day of the week. In this case, the first day of the week is monday. This is represented by first subtracting 0 with the weekday integer followed by adding 2 to the setDay.

Sunday = 1, Monday = 2, Tuesday = 3, Wednesday = 4, Thursday = 5, Friday = 6 and Saturday = 7. By adding more to this integers, you will go into the next week.

NSDateComponents *componentsToSubtract  = [[NSDateComponents alloc] init];
[componentsToSubtract setDay: (0 - [weekdayComponents weekday]) + 2];   
[componentsToSubtract setHour: 0 - [weekdayComponents hour]];
[componentsToSubtract setMinute: 0 - [weekdayComponents minute]];
[componentsToSubtract setSecond: 0 - [weekdayComponents second]];

Create date for first day in week

NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract toDate:today options:0];

By adding 6 to the date of the first day, we can get the last day, in our example Sunday.

NSDateComponents *componentsToAdd = [gregorian components:NSDayCalendarUnit fromDate:beginningOfWeek];
[componentsToAdd setDay:6];
NSDate *endOfWeek = [gregorian dateByAddingComponents:componentsToAdd toDate:beginningOfWeek options:0];

Hope this helps

Arnold
  • 41
  • 1
  • 6
1

Try this.

[components1 setDay:([components1 day]-([components1 weekday]-1) + 6)];

NSDate *endOfWeek1 = [gregorian11 dateFromComponents:components1];
kovpas
  • 9,553
  • 6
  • 40
  • 44
  • [components1 setDay:([components1 day]-([components1 weekday]-1) + 6)]; now =[gregorian dateFromComponents:components1]; [format setDateFormat:@"dd/MM/yyyy :EEEE"]; dateString = [format stringFromDate:now]; NSLog(@" week Last_date: %@", dateString); but above code output is== week Last_date: 29/04/2010 :Thursday – nivrutti Apr 27 '10 at 08:57
  • That's weird. You get the first day of week - this correct, but after you add 6 days to it, you get 4th day. Well, I'd suggest to add 9 instead of 6 - this should work. – kovpas Apr 27 '10 at 11:22
  • As a notion for all who read A.) You're tying this to one calendar, B.) You're hardcoding numbers with no guarantees of size and/or offsets. AKA, you should never say +6 and expect it to offset by the end of the week. – TheCodingArt Jul 30 '16 at 00:57
0
[components1 setDay:([components1 day]+([components1 weekday]+1))];

It will print

date: 27/04/2010 :Tuesday Weekend: 01/05/2010 :Saturday

I write "weekend" instead of First day....

Neelam Roy
  • 11
  • 1
0
NSDateComponents *components2 = [gregorian11 components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today1];

    [components1 setDay:(([components2 day]-([components2 weekday]-7)))];

    NSDate *now =[gregorian11 dateFromComponents:components2]; 
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"dd/MM/yyyy :EEEE"]; 
    NSString *dateString = [format stringFromDate:now]; 
    NSLog(@" week Last_date: %@", dateString);//here it gives me thursd
Maulik
  • 19,348
  • 14
  • 82
  • 137
  • 1
    Please add some comments to the code, it helps to understand you solution proposal. Check this [metaSO question](http://meta.stackexchange.com/questions/7656/how-do-i-write-a-good-answer-to-a-question) and [Jon Skeet: Coding Blog](http://msmvps.com/blogs/jon_skeet/archive/2009/02/17/answering-technical-questions-helpfully.aspx) on how to give a correct answer. – Yaroslav Nov 06 '12 at 13:05
-1

If you have an NSDate, you can use the current NSCalendar to retrieve that date's NSDateComponents. Set the NSDateComponents's weekday to 1 (the first day of the week), create a copy, and set the copy's weekday to 7 (the last day of the week). Then use the NSCalendar to convert both the NSDateComponents back to their respective NSDate objects, after which you can use an NSDateFormatter to create your string representation.

This link has an example about how to get a date's weekday:

http://stackoverflow.com/questions/1057349#1057405

Another approach as suggested by vikingosegundo to get both start and end days rangeOfUnit:startDate:interval:forDate:. It gives you the start and the interval for a certain time unit. With it it is easy to find the start of the week in the used calendar and add the range-1 to get the latest second in that week.

NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDate *startOfTheWeek;
NSDate *endOfWeek;
NSTimeInterval interval;
[cal rangeOfUnit:NSWeekCalendarUnit 
       startDate:&startOfTheWeek 
        interval:&interval 
         forDate:now];
//startOfWeek holds now the first day of the week, according to locale (monday vs. sunday)

endOfWeek = [startOfTheWeek dateByAddingTimeInterval:interval-1];
// holds 23:59:59 of last day in week.
Suresh Varma
  • 9,750
  • 1
  • 60
  • 91