我想要從系統當前日期開始的陣列中的最後7天(例如,FRI,THU,WED,TUE,MON,SUN,SAT,FRI)。如果有人有任何想法,請幫忙。感謝您的幫助。謝謝非常。如何從當前日期獲取數組中的最後7天?
回答
我假設這是一個普通的編程問題,而不是特定於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;
}
希望這有助於。
假設如果我有今天的指數,它是6,那麼應該如何執行循環請詳細說明,或者如果你舉一個例子會更好。 – 2012-04-13 16:38:22
2問題: 1.你想過去7天(如你所述)還是8(如你的例子所示)? 2.您是否希望當前位於第一位? – 2012-04-13 16:42:29
是的男人8天,如我所述。是的,我想在第一個位置的當天。 – 2012-04-13 16:45:43
我的兩分錢:
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);
這樣,你讓你的本地化的工作日名稱,你不必建立他們的自己的數組。
就這樣,它完全給你你所要求的。
我覺得這個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);
希望這有助於你。
一般來說,人們的答案站在自己的優點。如果你的答案不被接受,也許它會被其他人提高。 – trudyscousin 2012-04-13 17:44:25
@trudyscousin:是的,這是真的..:] – 2012-04-14 04:31:45
哈哈,當我在做其他一些事情時,你有很多答案,但是因爲我已經完成了大部分工作,所以無論如何它都是這樣。這種方法即使在時間變化等情況下也能工作,並提供本地化的日期名稱(如果您正在減去其他許多示例中的時間值,那麼當時間變化時,如果您接近時間更改,則會遇到問題....)
// 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
*)
*
*/
在您的示例中,有8天,而不是7 – Garoal 2012-04-13 17:57:22