1

I want last 7 days (e.g. FRI,THU,WED,TUE,MON,SUN,SAT,FRI) in an array from current date of the system.If someone has any idea please help.Help would be appreciated.Thank you very much.

RAMAN RANA
  • 1,785
  • 4
  • 21
  • 40

4 Answers4

5

My two cents:

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

This way you get your localized weekday names, and you don't have to build an array of them yourself.

As it is, it gives you exactly what you asked for.

Extra Savoir-Faire
  • 6,026
  • 4
  • 29
  • 47
2

Haha, well you got a bunch of answers while I was working on some other things but since I had mostly finished it, here it is anyway. This approach works even across time changes and such and provides localized day names. (If you are subtracting time values like in many of the other examples, you will run into problems when the time changes if you are close to the time change....)

// 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
 * )
 * 
 */
lnafziger
  • 25,760
  • 8
  • 60
  • 101
1

I am assuming that this is a general programming question and not specific to the iOS environment. If my assumption is incorrect, feel free to ignore this answer.

Build an array that has the days of the week. Find the index of today's day, Loop backwards over the array copying the content to the array you are creating, when you get to zero set the index to 6 (assuming zero based arrays) and loop back to today's day of the week.

Like this (this code is notional):

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

Hope this helps.

Joseph Carrigan
  • 359
  • 1
  • 5
0

I think this StackOverflow post is most useful:

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

Refer to answer of Can Berk Güder in there, It might help you on how to get day of the week:

You can use NSDateComponents for this:

unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [gregorian components:units fromDate:date]; then you can access individual parts of the date like this:

[components year]; [components month]; [components day]; Or, you can construct a new NSDate object using NSCalendar's dateFromComponents method, and compare the two NSDate objects.

Also do refer to this post below as it might help you on how you can get the logic to get last 7 days from current date:

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

Refer to answer of Ben S:

Adapted from the 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];

If you want the correct exact number of days depending on the month you should use an NSCalendar.

Also you can refer to equally nice answer in the same post by hasnat:

Might be a better way to write this but here what i came up on Ben's NSCalendar suggestion and working from there to 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);

Hope this helps you.

Community
  • 1
  • 1
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216