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;
}
The reason is the missing NSDayCalendarUnit component in both beginningOfDay and endOfDay methods.
In beginningOfDay and endOfDay functions you have missed NSCalendarUnitDay. Also NSMonthCalendarUnit, NSYearCalendarUnit are deprecated in ios8.
-(NSDate *)beginningOfDay:(NSDate *)date{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitDay| NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) 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:( NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitDay| NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
[components setHour:23];
[components setMinute:59];
[components setSecond:59];
return [cal dateFromComponents:components];
}
Related
I have searched for the solution to find the date of the sunday of the particular week.I have found questions that have been answered but it doesnt show the correct date of sunday.
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekday | NSCalendarUnitWeekOfYear;
NSDateComponents *comps = [calendar components:unitFlags fromDate:[NSDate date]];
comps.weekday = 1; //Sunday
comps.weekOfMonth = 1;
NSDate *sundayDate = [calendar dateFromComponents:comps];
When i print out the date it gives this.
2015-07-08 10:14:00 +0000
Its the current date.What am i doing wrong?
hope this helps you :
NSDate *today = [NSDate date];
NSCalendar *calenderObj = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
calenderObj.firstWeekday = 1; // Sunday = 1,
// NSDateComponents *components = [calenderObj components:NSWeekOfYearCalendarUnit fromDate:today];
NSDate *objDate = nil;
[calenderObj rangeOfUnit:NSWeekCalendarUnit startDate:&objDate interval:NULL forDate:today];
NSLog(#" === %#", objDate);
Output :
=== 2015-07-15 10:26:00 +0000
I'm trying to set the month and year of [NSDate date] but the function dateFromComponents return the wrong value, this is my code:
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian]; // Setup an NSCalendar
NSDateComponents *components = [gregorianCalendar components: NSUIntegerMax fromDate: [NSDate date]]; // Setup NSDateComponents
[components setTimeZone:[NSTimeZone defaultTimeZone]];
[components setMonth:monthInt];
[components setYear:yearInt];
// Setup the new date
NSDate *dateToCompare = [gregorianCalendar dateFromComponents: components];
yearInt is 2014 and monthInt is 12, but it reports back 12-2015 and not 2014, why is this?
Thanks!
you need to put only the bit operator you need, try this:
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian]; // Setup an NSCalendar
NSDateComponents *components = [gregorianCalendar components: NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear |NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitTimeZone fromDate: [NSDate date]]; // Setup NSDateComponents
[components setTimeZone:[NSTimeZone defaultTimeZone]];
[components setMonth:12];
[components setYear:2014];
// Setup the new date
NSDate *dateToCompare = [gregorianCalendar dateFromComponents: components];
NSLog(#"Ver %#",dateToCompare);
NSUIntegerMax = 18446744073709551615
In bit representation: 10000000000000000000000000000000000000000000000000000000000000000
To many switch off.
I am using
- (NSDate *)dateByAddingComponents:(NSDateComponents *)comps toDate:(NSDate *)date options:(NSCalendarOptions)opts
to calculate a new date, like below:
NSDateComponents *quaterComps = [NSDateComponents new];
quaterComps.quarter = 1;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *nextDate = [gregorian dateByAddingComponents:quaterComps toDate:firstDate options:0];
When firstDate is 2013-12-31 16:00:00 +0000,
Above API keeps returning the same result, not the next quarter date
** current date and next date **
NSDate *currentDate = [NSDate date];
NSDate *futureTime = [currentDate dateByAddingTimeInterval:60*60*72];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
[calendar setTimeZone:timeZone];
NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit fromDate:futureTime];
if ([components hour] >= 19) { // make it the next day
[components setDay:[components day] + 1 ];
}
[components setHour:8];
[components setMinute:00];
NSDate *alertTime = [calendar dateFromComponents:components];
NSDate *date = [NSDate date];
NSDateComponents *comp = [NSDateComponents new];
comp.weekOfYear = 3;
NSDate *date1 = [[NSCalendar currentCalendar] dateByAddingComponents:comp toDate:date options:0];
NSLog(#"date: %#", date);
NSLog(#"date1: %#", date1)
Is there any easier way to assign only hour and minute from one NSDate A to NSDate B, but keep B's Year Month and Day?
I don't know If it's an easier way, but it's the correct way:
NSDate *dateA = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSUInteger timeComps = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSTimeZoneCalendarUnit);
NSDateComponents *comps = [gregorian components:timeComps fromDate:dateA];
[components setHour: 3];
[components setMinute: 42];
NSDate *newDateB = [gregorian dateFromComponents: components];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit *dateComponent = (NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit);
NSDateComponents *components1 = [calendar components:dateComponent fromDate:dateA];
NSInteger day = components1.day;
NSInteger month = components1.month;
NSInteger year = components1.year;
NSDateComponents *components2 = [calendar components:dateComponent fromDate:dateB];
NSInteger hour = components2.hour;
NSInteger minute = components2.minute;
NSInteger second = components2.second;
NSDateComponents *requiredComponent = [[NSDateComponents alloc] init];
requiredComponent.day = day;
requiredComponent.month = month;
requiredComponent.year = year;
requiredComponent.hour = hour;
requiredComponent.minute = minute;
requiredComponent.second = second;
NSDate *date = [calendar dateFromComponents:requiredComponent];
I know there are some questions similar to this one, but I just can't find what's wrong with my code.
Basically what I want is if today is not sunday, currentDate is set to the last sunday.
If today is sunday I currentDate is set to the sunday before.
Here's how I'm trying to do it.
NSDateComponents *components = [[NSCalendar currentCalendar] components: NSWeekCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit fromDate:currentDate];
if (components.weekday == 1) { //Its sunday. We just need to subtract a week
[components setWeek: -1];
currentDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:currentDate options:0];
}else{ //If its not sunday we just go to sunday
[components setWeekday: 1];
currentDate = [[NSCalendar currentCalendar] dateFromComponents:components];
}
The first time this part of the code is executed I get the right answer. After the first time I get weird dates, like 02 Dec. 4026, and the year keeps going up.
Here's the code that made it work:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components: ( NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSYearCalendarUnit) fromDate:currentDate];
int weekday = components.weekday;
if (weekday == 1) {
[components setWeek:components.week -1];
}else{
[components setWeekday:1];
}
currentDate = [calendar dateFromComponents:components];
Use nextDateAfterDate with the .SearchBackwards option if you are using iOS 8.0+
let calendar = NSCalendar.currentCalendar()
let options: NSCalendarOptions = [.MatchPreviousTimePreservingSmallerUnits, .SearchBackwards]
calendar.nextDateAfterDate(NSDate(), matchingUnit: .Weekday, value: 1, options: options)
Try this category on NSCalendar
#interface NSCalendar (LastSunday)
-(NSDate *)previousSundayForDate:(NSDate *)date;
#end
#implementation NSCalendar (LastSunday)
-(NSDate *)previousSundayForDate:(NSDate *)date
{
static NSUInteger SUNDAY = 1;
static NSUInteger MONDAY = 2;
NSDate *startOfWeek;
[self rangeOfUnit:NSWeekCalendarUnit
startDate:&startOfWeek
interval:NULL
forDate:date];
if(self.firstWeekday == SUNDAY){
NSDate *beginningOfDate;
[self rangeOfUnit:NSDayCalendarUnit
startDate:&beginningOfDate
interval:NULL forDate:date];
if ([startOfWeek isEqualToDate:beginningOfDate]) {
startOfWeek = [self dateByAddingComponents:(
{
NSDateComponents *comps = [[NSDateComponents alloc] init];
comps.day = -7;
comps;
})
toDate:startOfWeek
options:0];
}
return startOfWeek;
}
if(self.firstWeekday == MONDAY)
return [self dateByAddingComponents:(
{
NSDateComponents *comps = [[NSDateComponents alloc] init];
comps.day = -1;
comps;
})
toDate:startOfWeek
options:0];
return nil;
}
#end
use it as:
NSDate *lastSunday = [calendar previousSundayForDate:[NSDate date]];
PS:
a version for "one-method-one-return-statement"- fetishists:
#implementation NSCalendar (LastSunday)
-(NSDate *)previousSundayForDate:(NSDate *)date
{
NSDate *resultDate = nil;
static NSUInteger SUNDAY = 1;
static NSUInteger MONDAY = 2;
NSDate *startOfWeek;
[self rangeOfUnit:NSWeekCalendarUnit
startDate:&startOfWeek
interval:NULL
forDate:date];
if(self.firstWeekday == SUNDAY){
NSDate *beginningOfDate;
[self rangeOfUnit:NSDayCalendarUnit
startDate:&beginningOfDate
interval:NULL forDate:date];
if ([startOfWeek isEqualToDate:beginningOfDate]) {
startOfWeek = [self dateByAddingComponents:(
{
NSDateComponents *comps = [[NSDateComponents alloc] init];
comps.day = -7;
comps;
})
toDate:startOfWeek
options:0];
}
resultDate = startOfWeek;
}
if(self.firstWeekday == MONDAY)
resultDate = [self dateByAddingComponents:(
{
NSDateComponents *comps = [[NSDateComponents alloc] init];
comps.day = -1;
comps;
})
toDate:startOfWeek
options:0];
return resultDate;
}
#end
The reason you got 4026 is because of this line:
NSDateComponents *components = [[NSCalendar currentCalendar] components: NSWeekCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit fromDate:currentDate];
It sets the year component to the current year, 2013. It also initializes the week and weekday components. Then the components are added to the current date, which doubles the year to 4026, here:
[components setWeek: -1];
currentDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:currentDate options:0];
If you want to subtract only one week, do it like this:
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setWeek: -1];
currentDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:currentDate options:0];
[components release];