Currently trying to use NSDateFormatter and NSDateComponents to set the week number for a month, then find the week of year that would be.
Let’s say we have a date: 20/05/2020, then set the week number to 0, or 1, or 2, etc.. then find out the week of year from that.
So far with NSDateComponents I’ve tried doing:
NSDate *date = self.date; // 20/05/2020
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
[components setWeekOfMonth:1];
NSDate *newDate = [calendar dateFromComponents:components];
NSInteger weekOfYear = [calendar components:NSCalendarUnitWeekOfYear fromDate:newDate].weekOfYear;
I'm always getting the output 18.
Expected output:
Change week of month to: 1 | output: 19 (4th)
Change week of month to: 2 | output: 20 (11th)
Change week of month to: 3 | output: 21 (18th)
Change week of month to: 4 | output: 22 (25th)
The problem is setting setWeekOfMonth in the date components has no effect.
Instead set the weekday and weekdayOrdinal
NSDate *date = self.date; // 20/05/2020
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
components.weekday = 2;
components.weekdayOrdinal = 3;
NSDate *newDate = [calendar dateFromComponents:components];
NSInteger weekOfYear = [calendar components:NSCalendarUnitWeekOfYear fromDate:newDate].weekOfYear;
This is a different approach:
It calculates the first monday in the current month, then it enumerates the next mondays until the end of the month.
- (NSArray *)yearOfWeeksInCurrentMonth
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components: NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
components.weekday = 2;
components.weekdayOrdinal = 1;
NSDate *firstMonday = [calendar dateFromComponents:components];
NSInteger weekOfYear = [calendar component:NSCalendarUnitWeekOfYear fromDate:firstMonday];
__block NSMutableArray *result = [NSMutableArray array];
[result addObject:#[firstMonday, #(weekOfYear)]];
NSDateComponents *weekComponents = [calendar components: NSCalendarUnitWeekday fromDate:firstMonday];
[calendar enumerateDatesStartingAfterDate:firstMonday matchingComponents:weekComponents options:NSCalendarMatchNextTime usingBlock:^(NSDate * _Nullable date, BOOL exactMatch, BOOL * _Nonnull stop) {
if (date != nil) {
if ( [calendar component:NSCalendarUnitMonth fromDate:date] > components.month ) {
*stop = YES;
} else {
NSInteger weekOfYear = [calendar component:NSCalendarUnitWeekOfYear fromDate:date];
[result addObject:#[date, #(weekOfYear)]];
}
}
}];
return [result copy];
}
The return value is an array containing the date (NSDate) and the corresponding weekOfYear (NSNumber)
(
(
"2020-05-03 22:00:00 +0000",
19
),
(
"2020-05-10 22:00:00 +0000",
20
),
(
"2020-05-17 22:00:00 +0000",
21
),
(
"2020-05-24 22:00:00 +0000",
22
)
)
Related
I'm writing a GUI for a legacy data object where a date is saved in the integer properties day of the year and year (real "genius" design...)
How can I convert this data into day and month given the year?
I'm currently trying:
int theYear = 2016;
int theDayOfTheYear = 222;
NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setYear:theYear];
[components setDay:theDayOfTheYear];
NSDate* theDate = [gregorian dateFromComponents:components];
This seems a bit too hacky to me. It could be just coincidence that it works with days >31 - haven't found any mention of this in the NSDateComponents class reference.
When I debug, po theDate gives me 2016-08-08 22:00:00 +0000
--> close, but it should be 2016-08-09 (!)
To perform the reverse operation, i.e. to convert a normal DD-MM-YYYY date into day of the year, I'm using this code:
NSDate *fullDate = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSYearCalendarUnit
forDate:fullDate];
But I can't seem to find the proper methods to do the opposite...
Sometimes a test is all takes :)
Don't forget that NSDate are GMT, so you might want to add [[NSTimeZone localTimeZone] secondsFromGMT] seconds to it.
It seems that you have it working :)
- (void) testDate
{
int startYear = 2016;
int startMonth = 1;
int startDay = 1;
// Create the startDate
NSDateComponents * startDateComp = [NSDateComponents new];
startDateComp.day = startDay;
startDateComp.month = startMonth;
startDateComp.year = startYear;
NSDate * startDate = [[NSCalendar currentCalendar] dateFromComponents:startDateComp];
for(int i=0 ; i < 365; i++)
{
// add i day to startDate
NSDate * testDate = [startDate dateByAddingTimeInterval:i * 24 * 60 * 60];
NSUInteger dayOfYear = [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSYearCalendarUnit
forDate:testDate];
XCTAssert(dayOfYear == (i + 1), #"the day of the year should be i + 1");
// Create a date using day of year
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setYear:startYear];
[components setDay:dayOfYear];
NSDate* resultDate = [[NSCalendar currentCalendar] dateFromComponents:components];
// testing result date against test date
NSInteger test, result;
test = [[NSCalendar currentCalendar] component:NSCalendarUnitDay fromDate:testDate];
result = [[NSCalendar currentCalendar] component:NSCalendarUnitDay fromDate:resultDate];
XCTAssert(test == result);
test = [[NSCalendar currentCalendar] component:NSCalendarUnitMonth fromDate:testDate];
result = [[NSCalendar currentCalendar] component:NSCalendarUnitMonth fromDate:resultDate];
XCTAssert(test == result);
test = [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:testDate];
result = [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:resultDate];
XCTAssert(test == result);
}
Please try this. Its the larger calendar unit NSCalendarUnitEra :)
NSDate *fullDate = [NSDate date];
NSCalendar *gregorian = [NSCalendar currentCalendar];
gregorian.timeZone = [NSTimeZone systemTimeZone];
NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSCalendarUnitDay
inUnit:NSCalendarUnitEra
forDate:fullDate];
NSDateComponents *dateCom = [[NSDateComponents alloc] init];
dateCom.day = dayOfYear;
NSDate *expectedDate = [gregorian dateFromComponents:dateCom];
NSLog(#"%#",expectedDate);
I am trying to set a NSDate from components; the hour is the only thing that I need to actually set, the remainder is from current date. This is my code:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
// put in date with start time
int month = [components month];
int day = [components day];
int year = [components year];
[components setHour: startTime];
[components setMinute: 0];
[components setSecond: 0];
NSDate *startDate = [gregorian dateFromComponents: components];
This is the result from the console:
Printing description of components:
Calendar Year: 2015
Month: 10
Leap month: no
Day: 3
Hour: 1730
Minute: 0
Second: 0
Printing description of startDate:
2015-10-25 15:57:12 +0000
Notice that the month and time have changed from what was supplied. My question is why did it change (for future reference) and what do I do to fix this?
I use this method for adding month to a date
- (NSDate *)sameDateByAddingMonths:(NSInteger)addMonths {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents * components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:self];
[components setMonth:components.month + addMonths];
return [calendar dateFromComponents:components];
}
But when the previous month in self NSDate, had more days, than it jumps on the first day next next month, like
June has 31 => self is June 31
Calling this, sets the date to 1.August, since July has 30 days
How to get this right? i thought this should behave "right" and clip on the end of month
That's what dateByAddingComponents is for:
- (NSDate *)sameDateByAddingMonths:(NSInteger)addMonths {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setMonth:addMonths];
return [calendar dateByAddingComponents:components toDate:self options:0];
}
You can use such a method to solve the issue, here I'm adding 18 months to a date:
+(NSDate *)addEighteenMonthsToDate:(NSDate *)startDate {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *newDate = [startDate copy];
newDate = [calendar dateByAddingUnit:NSCalendarUnitYear value:1 toDate:startDate options:0];
newDate = [calendar dateByAddingUnit:NSCalendarUnitMonth value:6 toDate:newDate options:0];
return newDate; }
how can I calculate the number of calendarweeks in objective-C for a given year.
I tried:
[calendar rangeOfUnit:NSWeekOfYearCalendarUnit inUnit: NSYearCalendarUnit forDate: [NSDate date]].length
but it returns 54.
Thanks
You are using NSWeekOfYearCalendarUnit, so you must use the corresponding larger unit which is NSYearForWeekOfYearCalendarUnit.
NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.firstWeekday = 2;
calendar.minimumDaysInFirstWeek = 4;
int n = [calendar rangeOfUnit:NSWeekOfYearCalendarUnit inUnit:NSYearForWeekOfYearCalendarUnit forDate: [NSDate date]].length;
NSLog(#"%d", n); // 52
Finally, note that both NSWeekOfYearCalendarUnit and NSYearForWeekOfYearCalendarUnit are iOS 5.0 and OS X 10.7 only.
Edit
As noted by #lnafziger, if you use a date that is in a calendar week from the previous year or next year such as 1/1/2016, this will calculate the number of weeks in that year (2015 in the example), and not the actual year of the date (2016 in the example). If this is not what you want, you can change the date like follows:
NSDateComponents *components = [calendar components:NSYearCalendarUnit fromDate:date];
components.month = 3;
date = [calendar dateFromComponents:components];
After a significant amount of testing, here is a function which will return it for any year:
- (NSUInteger)iso8601WeeksForYear:(NSUInteger)year {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *firstThursdayOfYearComponents = [[NSDateComponents alloc] init];
[firstThursdayOfYearComponents setWeekday:5]; // Thursday
[firstThursdayOfYearComponents setWeekdayOrdinal:1]; // The first Thursday of the month
[firstThursdayOfYearComponents setMonth:1]; // January
[firstThursdayOfYearComponents setYear:year];
NSDate *firstThursday = [calendar dateFromComponents:firstThursdayOfYearComponents];
NSDateComponents *lastDayOfYearComponents = [[NSDateComponents alloc] init];
[lastDayOfYearComponents setDay:31];
[lastDayOfYearComponents setMonth:12];
[lastDayOfYearComponents setYear:year];
NSDate *lastDayOfYear = [calendar dateFromComponents:lastDayOfYearComponents];
NSDateComponents *result = [calendar components:NSWeekCalendarUnit fromDate:firstThursday toDate:lastDayOfYear options:0];
return result.week + 1;
}
Basically, per the spec, the total number of weeks is the same as the total number of Thursday's in the year, which is what this calculates.
I have tested it for the entire 400 year cycle (starting in the year 0 and the year 2000) and all cases match the spec.
Hello
I have a problem with setting a firstWeekDay, here is what I do:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit) fromDate:targetDate];
[comp setWeekday:2];
NSDate *firstWeekDay = [gregorian dateFromComponents:comp];
If Saturday 2011-01-01 is targetDate, the firstWeekDay in my calendar appears to be Monday 2011-12-26, but in fact it should be Monday 2010-12-26. How can I make it right?
To fix this problem, Add NSYearForWeekOfYearCalendarUnit in the NSDateComponents.
Ex:
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate];
[comps setWeekday:2]; // 2: monday
firstDayOfTheWeek = [calendar dateFromComponents:comps];
[comps setWeekday:7]; // 7: saturday
lastDayOfTheWeek = [calendar dateFromComponents:comps];
Try this
// adjust them for first day of previous week (Monday)
[comp setWeekday:2];
[comp setWeek:([comp week] - 1)];
Note this solution assumes the first day of the week is Monday.
Below also covers the edge case,
- (NSDate *)getFirstDayOfTheWeekFromDate:(NSDate *)givenDate
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:givenDate];
[components setWeekday:2]; // 1 == Sunday, 7 == Saturday
if([[calendar dateFromComponents:components] compare: curDate] == NSOrderedDescending) // if start is later in time than end
{
[components setWeek:[components week]-1];
}
return [calendar dateFromComponents:components];
}
The Swift solution (thanks #YvesJusot):
let calendar = NSCalendar.currentCalendar()
let comps = calendar.components(NSCalendarUnit.WeekOfYearCalendarUnit|NSCalendarUnit.YearCalendarUnit|NSCalendarUnit.MonthCalendarUnit|NSCalendarUnit.WeekCalendarUnit|NSCalendarUnit.WeekdayCalendarUnit, fromDate: NSDate()) as NSDateComponents
comps.setValue(2, forComponent: NSCalendarUnit.WeekdayCalendarUnit)
let monday = calendar.dateFromComponents(comps)!
println("\(monday)")
You suppose to use dynamic values instead of hardcoding 2 as Monday!
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:[NSDate date]];
comps.weekday = calendar.firstWeekday;
firstDayOfTheWeek = [calendar dateFromComponents:comps];
comps.weekday = calendar.firstWeekday - 1;
lastDayOfTheWeek = [calendar dateFromComponents:comps];
I did some changes in #JasonGarrett answer and it works for me.
let calendar = NSCalendar.currentCalendar()
let flags : NSCalendarUnit = NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitYear
let comps = calendar.components(flags, fromDate: NSDate()) as NSDateComponents
comps.setValue(2, forComponent: NSCalendarUnit.CalendarUnitWeekday)
let monday = calendar.dateFromComponents(comps)!
// Make these two methods as categories to NSDate
// Use This to get First date of the week (Monday)
- (NSDate *) getFirstDayOfWeek {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *compOfDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday) fromDate:self];
NSInteger numOfDaysToAdd = compOfDate.weekday == 1 ? -6 : (2 - compOfDate.weekday);
NSDateComponents *compToAdd = [[NSDateComponents alloc] init];
compToAdd.day = numOfDaysToAdd;
return [calendar dateByAddingComponents:compToAdd toDate:self options:0];
}
// Use This to get last date of the week (Sunday)
- (NSDate *)getLastDayOfWeek {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *compOfDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday) fromDate:self];
NSInteger numOfDaysToAdd = compOfDate.weekday == 1 ? 0 : (8 - compOfDate.weekday);
NSDateComponents *compToAdd = [[NSDateComponents alloc] init];
compToAdd.day = numOfDaysToAdd;
return [calendar dateByAddingComponents:compToAdd toDate:self options:0];
}