2012-04-13 93 views

回答

1

我假设这是一个普通的编程问题,而不是特定于iOS环境。如果我的假设不正确,请随时忽略此答案。

构建一个有星期几的数组。 找到当天的索引, 向后循环将数组复制到您正在创建的数组中,当您达到零时,将索引设置为6(假设为零的数组)并循环回到当天的星期几。

这样的(这个代码是名义上的):

string[] days = {"Mon", "Tues" .... "Sun"}; 
string[] last8Days = new string[8]; 
int daysIndex, last8DaysIndex; 
daysIndex = \\some code to get the index of today's day. 
for (last8DaysIndex = 0; last8DaysIndex < 8; last8DaysIndex++) 
{ 
    last8Days[last8DaysIndex] = days[daysIndex]; 
    daysIndex--; 
    if(daysIndex < 0) 
     daysIndex = 6; 
} 

希望这有助于。

+0

假设如果我有今天的指数,它是6,那么应该如何执行循环请详细说明,或者如果你举一个例子会更好。 – 2012-04-13 16:38:22

+0

2问题: 1.你想过去7天(如你所述)还是8(如你的例子所示)? 2.您是否希望当前位于第一位? – 2012-04-13 16:42:29

+0

是的男人8天,如我所述。是的,我想在第一个位置的当天。 – 2012-04-13 16:45:43

5

我的两分钱:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
[dateFormatter setDateFormat:@"EEE"]; 

NSDate *now = [NSDate date]; 

NSMutableArray *results = [NSMutableArray arrayWithCapacity:8]; 

for (int i = 0; i < 8; i++) 
{ 
    NSDate *date = [NSDate dateWithTimeInterval:-(i * (60 * 60 * 24)) sinceDate:now]; 
    [results addObject:[dateFormatter stringFromDate:date]]; 
} 

NSLog(@"%@", results); 

这样,你让你的本地化的工作日名称,你不必建立他们的自己的数组。

就这样,它完全给你你所要求的。

0

我觉得这个StackOverflow的职位是最有用的:

How to check what day of the week it is (i.e. Tues, Fri?) and compare two NSDates?

参考在那里回答灿伯克Güder的,它可以帮助你如何获得星期:

您可以使用NSDateComponents此:

无符号单位= NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents * components = [格里高利组件:units fromDate:date];那么你可以访问像这样的日期的个别部分:

[元件年]; [组件月份]; [部件日];或者,您可以使用NSCalendar的dateFromComponents方法 构造一个新的NSDate对象,并比较两个NSDate对象。

也可做参考这个帖子下面,因为它可以帮助你在如何可以得到逻辑与去年7天当前日期得到:

get NSDate today, yesterday, this Week, last Week, this Month, last Month... variables

参见回答本小号

Date and Time Programming Guide改编:

// Right now, you can remove the seconds into the day if you want 
NSDate *today = [NSDate date]; 

// All intervals taken from Google 
NSDate *yesterday = [today dateByAddingTimeInterval: -86400.0]; 
NSDate *thisWeek = [today dateByAddingTimeInterval: -604800.0]; 
NSDate *lastWeek = [today dateByAddingTimeInterval: -1209600.0]; 

// To get the correct number of seconds in each month use NSCalendar 
NSDate *thisMonth = [today dateByAddingTimeInterval: -2629743.83]; 
NSDate *lastMonth = [today dateByAddingTimeInterval: -5259487.66]; 

如果你想取决于月份 你应该使用一个NSCalendar天的正确的具体数量。

您也可以参考同样漂亮的答案在同一职位由hasnat

可能是一个更好的方式来写这一点,但在这里我想出了在Ben的 NSCalendar建议和工作从那里到NSDateComponents

NSCalendar *cal = [NSCalendar currentCalendar]; 
NSDateComponents *components = [cal components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[[NSDate alloc] init]]; 

[components setHour:-[components hour]]; 
[components setMinute:-[components minute]]; 
[components setSecond:-[components second]]; 
NSDate *today = [cal dateByAddingComponents:components toDate:[[NSDate alloc] init] options:0]; //This variable should now be pointing at a date object that is the start of today (midnight); 

[components setHour:-24]; 
[components setMinute:0]; 
[components setSecond:0]; 
NSDate *yesterday = [cal dateByAddingComponents:components toDate: today options:0]; 

components = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[[NSDate alloc] init]]; 

[components setDay:([components day] - ([components weekday] - 1))]; 
NSDate *thisWeek = [cal dateFromComponents:components]; 

[components setDay:([components day] - 7)]; 
NSDate *lastWeek = [cal dateFromComponents:components]; 

[components setDay:([components day] - ([components day] -1))]; 
NSDate *thisMonth = [cal dateFromComponents:components]; 

[components setMonth:([components month] - 1)]; 
NSDate *lastMonth = [cal dateFromComponents:components]; 

NSLog(@"today=%@",today); 
NSLog(@"yesterday=%@",yesterday); 
NSLog(@"thisWeek=%@",thisWeek); 
NSLog(@"lastWeek=%@",lastWeek); 
NSLog(@"thisMonth=%@",thisMonth); 
NSLog(@"lastMonth=%@",lastMonth); 

希望这有助于你。

+1

一般来说,人们的答案站在自己的优点。如果你的答案不被接受,也许它会被其他人提高。 – trudyscousin 2012-04-13 17:44:25

+0

@trudyscousin:是的,这是真的..:] – 2012-04-14 04:31:45

2

哈哈,当我在做其他一些事情时,你有很多答案,但是因为我已经完成了大部分工作,所以无论如何它都是这样。这种方法即使在时间变化等情况下也能工作,并提供本地化的日期名称(如果您正在减去其他许多示例中的时间值,那么当时间变化时,如果您接近时间更改,则会遇到问题....)

// Subtract one day from the current date (this compensates for daylight savings time, etc, etc.) 
- (NSDate *)dateBySubtractingOneDayFromDate:(NSDate *)date { 
    NSCalendar *cal = [NSCalendar currentCalendar]; 

    NSDateComponents *minusOneDay = [[NSDateComponents alloc] init]; 
    [minusOneDay setDay:-1]; 
    NSDate *newDate = [cal dateByAddingComponents:minusOneDay 
              toDate:date 
              options:NSWrapCalendarComponents]; 
    return newDate; 
} 

- (NSArray *)lastSevenDays { 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"EEE"]; 

    NSDate *date = [NSDate date]; 
    NSMutableArray *weekDays = [[NSMutableArray alloc] initWithCapacity:8]; 
    for (int i = 0; i > -8; i--) { 
     NSString *weekDay = [formatter stringFromDate:date]; 
     [weekDays addObject:weekDay]; 
     date = [self dateBySubtractingOneDayFromDate:date]; 
    } 
    return weekDays; 
} 


// To use it, do this: 
NSLog(@"%@", [self lastSevenDays]); 
/* 
* Results: 
* (
*  Fri, 
*  Thu, 
*  Wed, 
*  Tue, 
*  Mon, 
*  Sun, 
*  Sat, 
*  Fri 
*) 
* 
*/ 
相关问题