2012-04-13 9 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ループの実装方法を教えてくださいまたは場合は、例を挙げると良いでしょう。 –

+0

2質問: 1.あなたは最後の7日間(あなたの状態)または8日(あなたの例が示すように)をしますか? 2.現在の日を最初の位置にしますか? –

+0

私は述べたようにはい人8日。はい、私は最初の位置に現在の日をしたいです。 –

5

私の2セント:

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 *コンポーネント= [グレゴリオ暦コンポーネント:fromDate:date];次のように日付の個々の部分にアクセスできます。

[components year]; [コンポーネント月]; [コンポーネント日]。または、 NSCalendarのdateFromComponents メソッドを使用して新しいNSDateオブジェクトを構築し、2つのNSDateオブジェクトを比較できます。

それはあなたが現在の日付から最後の7日間を取得するためのロジックを得ることができる方法にあなたを助けるかもしれないとしても、以下のこの記事を参照してください:

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

の答えを参照してくださいベン・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によって同じポストでの素敵な答えを参照することができます:私はベンの NSCalendar提案と作業に思い付いたものをここに

がこれを書くためのより良い方法かもしれないが、そこから希望

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); 

NSDateComponents

にこれは役立ちます。

+1

一般的に、自分のメリットには答えがあります。あなたの答えが受け入れられない場合、多分それは誰かによってupvotedされます。 – trudyscousin

+0

@trudyscousin:Yes thats true ..:] –

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 
*) 
* 
*/ 
関連する問題