How to find the last day of the current week in iphone?

In my application, I use the following codes to extract the 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 the code above I received only the current day and date and the first day of the week , but "I need to get the last day of the week."

Where am I mistaken in my code and what modification is needed to get the last day / date of the week.

Please help me urgently.

Thank you very much in advance

+3
source share
5 answers

NSDate, NSCalendar NSDateComponents. NSDateComponents 1 ( ), 7 ( ). NSCalendar, NSDateComponents NSDate, NSDateFormatter .

, :

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

, vikingosegundo rangeOfUnit:startDate:interval:forDate:. . -1, .

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

:

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

, , . - . 0 , 2 setDay.

= 1, = 2, = 3, = 4, = 5, = 6 = 7. , .

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]];

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

6 , .

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

,

+3

Try it.

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

NSDate *endOfWeek1 = [gregorian11 dateFromComponents:components1];
+1
source
[components1 setDay:([components1 day]+([components1 weekday]+1))];

He will print

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

I write "weekend" instead of the first day ....

0
source
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
0
source

Source: https://habr.com/ru/post/1742938/


All Articles