如何在iphone上找到上周的最后一天?

时间:2022-10-21 12:54:28

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.

我的代码有错误,需要做哪些修改才能得到最后一天/每周的日期。

Please help me out its urgent.

请帮我看一下紧急情况。

Thanks a lot in advance

非常感谢。

5 个解决方案

#1


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

如果您有一个NSDate,您可以使用当前的NSCalendar来检索该日期的NSDateComponents。将NSDateComponents的工作日设置为1(一周的第一天),创建一个副本,并将副本的工作日设置为7(一周的最后一天)。然后使用NSCalendar将两个NSDateComponents转换回各自的NSDate对象,之后可以使用NSDateFormatter来创建字符串表示。

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

这个链接有一个关于如何获得日期工作日的例子:

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

http://*.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.

vikingosegundo建议的另一种方法是获得开始和结束的天数范围。它给出一个特定时间单位的开始和间隔。有了它,就很容易在使用的日历中找到一周的开始,并添加范围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.

#2


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.

计算从今天开始要减的天数,以得到一周的第一天。在这种情况下,一周的第一天是星期一。这表示为先用工作日整数减去0,然后在setDay中增加2。

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.

星期日= 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]];

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.

通过在第一天的日期增加6,我们可以得到最后一天,在我们的示例Sunday中。

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

Hope this helps

希望这有助于

#3


1  

Try this.

试试这个。

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

NSDate *endOfWeek1 = [gregorian11 dateFromComponents:components1];

#4


0  

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

It will print

它将打印

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

日期:2010年4月27日:周二周末:2010年5月1日:周六

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

我写“周末”而不是第一天....

#5


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

#1


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

如果您有一个NSDate,您可以使用当前的NSCalendar来检索该日期的NSDateComponents。将NSDateComponents的工作日设置为1(一周的第一天),创建一个副本,并将副本的工作日设置为7(一周的最后一天)。然后使用NSCalendar将两个NSDateComponents转换回各自的NSDate对象,之后可以使用NSDateFormatter来创建字符串表示。

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

这个链接有一个关于如何获得日期工作日的例子:

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

http://*.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.

vikingosegundo建议的另一种方法是获得开始和结束的天数范围。它给出一个特定时间单位的开始和间隔。有了它,就很容易在使用的日历中找到一周的开始,并添加范围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.

#2


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.

计算从今天开始要减的天数,以得到一周的第一天。在这种情况下,一周的第一天是星期一。这表示为先用工作日整数减去0,然后在setDay中增加2。

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.

星期日= 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]];

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.

通过在第一天的日期增加6,我们可以得到最后一天,在我们的示例Sunday中。

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

Hope this helps

希望这有助于

#3


1  

Try this.

试试这个。

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

NSDate *endOfWeek1 = [gregorian11 dateFromComponents:components1];

#4


0  

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

It will print

它将打印

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

日期:2010年4月27日:周二周末:2010年5月1日:周六

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

我写“周末”而不是第一天....

#5


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