Objective c 如何从当前日期获取阵列中的最后7天?

Objective c 如何从当前日期获取阵列中的最后7天?,objective-c,ios4,Objective C,Ios4,我想要从系统当前日期开始的最后7天(如周五、周四、周三、周二、周一、周日、周六、周五)。如果有人有任何想法,请提供帮助。将非常感谢您的帮助。非常感谢 我假设这是一个一般的编程问题,而不是特定于iOS环境。如果我的假设不正确,请忽略这个答案 构建一个包含一周中的天数的数组。 找到今天的索引, 在数组上向后循环,将内容复制到您正在创建的数组中,当您达到零时,将索引设置为6(假设基于零的数组),并循环回一周中的今天 这样(此代码是概念性的): string[]天={“周一”、“周二”…“周日”}; 字

我想要从系统当前日期开始的最后7天(如周五、周四、周三、周二、周一、周日、周六、周五)。如果有人有任何想法,请提供帮助。将非常感谢您的帮助。非常感谢

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

构建一个包含一周中的天数的数组。 找到今天的索引, 在数组上向后循环,将内容复制到您正在创建的数组中,当您达到零时,将索引设置为6(假设基于零的数组),并循环回一周中的今天

这样(此代码是概念性的):

string[]天={“周一”、“周二”…“周日”};
字符串[]last8Days=新字符串[8];
int daysIndex,last8DaysIndex;
daysIndex=\\n获取今天索引的一些代码。
对于(last8DaysIndex=0;last8DaysIndex<8;last8DaysIndex++)
{
Last8天[Last8天索引]=天[daysIndex];
daysIndex--;
如果(daysIndex<0)
daysIndex=6;
}

希望这有帮助。

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

构建一个包含一周中的天数的数组。 找到今天的索引, 在数组上向后循环,将内容复制到您正在创建的数组中,当您达到零时,将索引设置为6(假设基于零的数组),并循环回一周中的今天

这样(此代码是概念性的):

string[]天={“周一”、“周二”…“周日”};
字符串[]last8Days=新字符串[8];
int daysIndex,last8DaysIndex;
daysIndex=\\n获取今天索引的一些代码。
对于(last8DaysIndex=0;last8DaysIndex<8;last8DaysIndex++)
{
Last8天[Last8天索引]=天[daysIndex];
daysIndex--;
如果(daysIndex<0)
daysIndex=6;
}
希望这有帮助。

我的两分钱:

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);
NSDateFormatter*dateFormatter=[[NSDateFormatter alloc]init]autorelease];
[日期格式化程序setDateFormat:@“EEE”];
NSDate*现在=[NSDate日期];
NSMutableArray*结果=[NSMutableArray阵列容量:8];
对于(int i=0;i<8;i++)
{
NSDate*date=[NSDate date WITH TIME INTERVAL:-(i*(60*60*24))自终止日期:现在];
[结果添加对象:[dateFormatter stringFromDate:date]];
}
NSLog(@“%@”,结果);
这样,您就可以获得本地化的工作日名称,而不必自己构建它们的数组

事实上,它给了你想要的东西。

我的两分钱:

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);
NSDateFormatter*dateFormatter=[[NSDateFormatter alloc]init]autorelease];
[日期格式化程序setDateFormat:@“EEE”];
NSDate*现在=[NSDate日期];
NSMutableArray*结果=[NSMutableArray阵列容量:8];
对于(int i=0;i<8;i++)
{
NSDate*date=[NSDate date WITH TIME INTERVAL:-(i*(60*60*24))自终止日期:现在];
[结果添加对象:[dateFormatter stringFromDate:date]];
}
NSLog(@“%@”,结果);
这样,您就可以获得本地化的工作日名称,而不必自己构建它们的数组


事实上,它给了你想要的东西。

我认为这篇StackOverflow帖子非常有用:

请参考Can Berk Güder的答案,它可能会在如何获得一周中的某一天方面帮助您:

您可以为此使用NSDATE组件:

无符号单位=NSYearCalendarUnit | NSMonthCalendarUnit | 国家统计局;NSCalendar*格里高利=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];NSDATE组件 *组件=[公历组件:单位fromDate:date];然后您可以访问日期的各个部分,如下所示:

[组成部分年];[组成部分月];[组件日];或者,你可以 使用NSCalendar的dateFromComponents构建新的NSDate对象 方法,并比较两个NSDate对象

也请参考下面的这篇文章,因为它可能会帮助您了解如何获得从当前日期算起最后7天的逻辑:

请参阅Ben S的回答:

改编自:

如果您希望根据月份获得正确的确切天数 你应该使用一个

你也可以在同一篇文章中通过hasnat找到同样好的答案:

也许是写这篇文章的更好的方式,但这里是我在Ben的 NSCalendar建议,并从那里工作到NSDateComponents


希望这对您有所帮助。

我认为这篇StackOverflow文章非常有用:

请参考Can Berk Güder的答案,它可能会在如何获得一周中的某一天方面帮助您:

您可以为此使用NSDATE组件:

无符号单位=NSYearCalendarUnit | NSMonthCalendarUnit | 国家统计局;NSCalendar*格里高利=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];NSDATE组件 *组件=[公历组件:单位fromDate:date];然后您可以访问日期的各个部分,如下所示:

[组成部分年];[组成部分月];[组件日];或者,你可以 使用NSCalendar的dateFromComponents构建新的NSDate对象 方法,并比较两个NSDate对象

也请参考下面的这篇文章,因为它可能会帮助您了解如何获得从当前日期算起最后7天的逻辑:

请参阅Ben S的回答:

改编自:

如果您希望根据月份获得正确的确切天数 你应该使用一个

你也可以在同一篇文章中通过hasnat找到同样好的答案:

也许是写这篇文章的更好的方式,但这里是我在Ben的 NSCalendar建议,并从那里工作到NSDateComponents


希望这能对你有所帮助。

哈哈,当我在这里时,你得到了一大堆答案
// 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 *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);
// 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
 * )
 * 
 */