I have tried changing my functions that get the start of day/end of day NSDates several times and I know the loop is working bc its printing out the right days of the week, but the start_timestamp and end_timestamp for each day is showing up the same for some reason. It has to be something involving the int not changing or something not directly related to the logic im not seeing:
Here is what its printing out:
WEEK_ARRAY:{
Friday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
Monday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
Saturday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
Sunday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
Thursday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
Tuesday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
Wednesday = {
"DAY_END" = 1454389199;
"DAY_START" = 1454302800;
};
}
Here is the logic:
- (NSMutableDictionary *)lastSevenDays {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEEE"];
NSDate *date = [NSDate date];
NSMutableDictionary *weekDays = [[NSMutableDictionary alloc] init];
for (int i = 0; i <7; i++) {
NSString *weekDay = [formatter stringFromDate:date];
date = [self dateByAddingOneDayFromDate:date];
NSMutableDictionary *specificDayDict=[[NSMutableDictionary alloc]init];
NSDate *StartNSDate=[self beginningOfDay:date];
NSNumber *StartTstamp=[NSNumber numberWithInt:[self convertNSDateToTimestamp:StartNSDate]];
int endOfDay=[self convertNSDateToTimestamp:[self endOfDay:date]];
NSLog(@"DAY:%@ | DAY_START:%@ | DAY_END:%d",weekDay,StartTstamp,endOfDay);
[specificDayDict setValue:StartTstamp forKey:@"DAY_START"];
[specificDayDict setValue:[NSNumber numberWithInt:endOfDay] forKey:@"DAY_END"];
[weekDays setObject:specificDayDict forKey:weekDay];
}
return weekDays;
}
- (NSDate *)dateByAddingOneDayFromDate:(NSDate *)date {
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *plusOneDay = [[NSDateComponents alloc] init];
[plusOneDay setDay:+1];
NSDate *newDate = [cal dateByAddingComponents:plusOneDay
toDate:date
options:NSWrapCalendarComponents];
return newDate;
}
-(NSDate *)beginningOfDay:(NSDate *)date
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:date];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
return [cal dateFromComponents:components];
}
-(NSDate *)endOfDay:(NSDate *)date
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:date];
[components setHour:23];
[components setMinute:59];
[components setSecond:59];
return [cal dateFromComponents:components];
}
-(NSDate *)convertTimestampToNSDate:(int)timestamp{
NSDate* date = [NSDate dateWithTimeIntervalSince1970:timestamp];
return date;
}
-(int )convertNSDateToTimestamp:(NSDate *)date{
int timestamp=[date timeIntervalSince1970];
return timestamp;
}