0

我正在尝试获取本周星期一至星期四的日期。但是由于某种原因,这段代码给了我本周日期和前几周日期的混合。

我知道这可能是一种奇怪的做法,但我真的不知道代码有什么问题。

如果有人有更好的方法来获取日期,请给出提示:)

我的代码:

NSDate *today = [NSDate date];


//Change first weekday of the week

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian.firstWeekday = 2;

NSCalendar *gregorian1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian1.firstWeekday = 3;

NSCalendar *gregorian2 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian2.firstWeekday = 4;

NSCalendar *gregorian3 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian3.firstWeekday = 5;




NSDateComponents *components = [gregorian components:NSWeekCalendarUnit fromDate:today];

NSUInteger weekOfYear = [components weekOfYear];

//Give the date of the first week day

NSDate *mondaysdate = nil;
[gregorian rangeOfUnit:NSWeekCalendarUnit startDate:&mondaysdate interval:NULL forDate:today];
NSDate *tuesdaysdate = nil;
[gregorian1 rangeOfUnit:NSWeekCalendarUnit startDate:&tuesdaysdate interval:NULL forDate:today];

NSDate *wednesdaysdate = nil;
[gregorian2 rangeOfUnit:NSWeekCalendarUnit startDate:&wednesdaysdate interval:NULL forDate:today];
NSDate *thursdaysdate = nil;
[gregorian3 rangeOfUnit:NSWeekCalendarUnit startDate:&thursdaysdate interval:NULL forDate:today];


//Making the dates into string

NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init];
formatter2.dateFormat = @"yyyy/MM/dd";

NSString *monday = [formatter2 stringFromDate:mondaysdate];
NSString *tuesday = [formatter2 stringFromDate:tuesdaysdate];
NSString *wednesday = [formatter2 stringFromDate:wednesdaysdate];
NSString *thursday = [formatter2 stringFromDate:thursdaysdate];
4

2 回答 2

0

您可以使用以下内容获取星期几作为数字,这是伪代码,因此可能无法编译,但它应该给您一个良好的开端。根据数字,您可以生成其他日期,然后稍后将它们作为字符串返回。

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"c"]; //<-  gives you day of week as an integer, 1 for monday, 2 for tuesday, etc

NSDate *today = [NSDate date];
NSInteger dayOfWeek = [[df stringFromDate:today] integerValue];

然后通过将日期偏移量添加到您的原始今天变量来创建其他 NSDates。

然后将它们格式化。

[df setDateFormat:@"yyyy/mm/dd"];
[df stringFromDate:today]; //<-- do this for all the other dates too

[df release]; //<- if you do this method often, consider having the date formatter as global

这是格式化的一个很好的参考。 http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

于 2012-11-05T22:49:44.523 回答
0
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorianCalendar components:(NSYearCalendarUnits | NSMonthCalendarUnits | NSWeekCalendarUnits | NSWeekdayCalendarUnits) fromDate:[NSDate date]];

comps.weekday = 2;
NSDate *monday = [gregorianCalendar dateFromComponents:comps];

comps.weekday = 3;
NSDate *tuesday = [gregorianCalendar dateFromComponents:comps];

comps.weekday = 4;
NSDate *wednesday = [gregorianCalendar dateFromComponents:comps];

comps.weekday = 5;
NSDate *thursday = [gregorianCalendar dateFromComponents:comps];
于 2012-11-05T22:56:41.153 回答