我希望过去7天(例如FRI,THU,WED,TUE,MON,SUN,SAT,FRI)在系统当前日期的数组中。如果有人有任何想法,请帮助。帮助将不胜感激。谢谢你非常喜欢
答案 0 :(得分: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);
通过这种方式,您可以获得本地化的工作日名称,而无需自己构建数组。
实际上,它可以为您提供您所要求的内容。
答案 1 :(得分: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
* )
*
*/
答案 2 :(得分: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;
}
希望这有帮助。
答案 3 :(得分:0)
我认为这篇StackOverflow帖子最有用:
How to check what day of the week it is (i.e. Tues, Fri?) and compare two NSDates?
请参阅 CanBerkGüder的回答,它可以帮助您如何获得星期几:
您可以使用NSDateComponents:
unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents * components = [gregorian components:units fromDate:date];那么您可以像这样访问日期的各个部分:
[组件年份]; [组件月]; [组件日];或者,你可以 使用NSCalendar的dateFromComponents构造一个新的NSDate对象 方法,并比较两个NSDate对象。
另请参阅下面的这篇文章,因为它可以帮助您如何从当前日期获得过去7天的逻辑:
get NSDate today, yesterday, this Week, last Week, this Month, last Month... variables
参考 Ben S :
的回答改编自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);
希望这会对你有所帮助。