I used
[currentComps setFirstWeekday:2];
It returns the number of the day of week starting with Sunday. How should I hardcode returning this number starting with Monday?
Contrary to the comment by Martin R, you can use NSCalendar to specify the first day of the week via:
- (void)setFirstWeekday:(NSUInteger)weekday
Reading the documentation of NSCalendar, it states the method setFirstWeekday:
Sets the index of the first weekday for the receiver.
I haven't tried this myself, but I would assume that creating an instance of NSCalendar, calling the aforementioned method and also calling setCalendar: on your NSDateComponents would attenuate all dates components returned to work as you intend.
Related
I want to define a calendar year with the ISO 8601 standard. Not a specific year, but the general duration. This has to stand for something that has a duration of exactly one calendar year, that starts on the first of January and ends on December 31.
It is not P1Y, I think, because that could be the duration of a year starting anytime. I can only come up something like: P01/12, but that seems ambiguous or not according to the standard syntax.
You may try:
--01-01/P1Y
It’s not very clear to me whether it’s valid, but I think it follows the logic of the ISO 8601 standard. it’s a time interval that begins on January 1 with no year specified and lasts a year.
A time interval is pretty clearly what you are after. According to Wikipedia:
There are four ways to express a time interval:
Start and end, such as "2007-03-01T13:00:00Z/2008-05-11T15:30:00Z"
Start and duration, such as "2007-03-01T13:00:00Z/P1Y2M10DT2H30M"
Duration and end, such as "P1Y2M10DT2H30M/2008-05-11T15:30:00Z"
Duration only, such as "P1Y2M10DT2H30M", with additional context information
So I have taken number 2. from the list.
Link: Wikipedia article ISO 8601, section Time intervals
The method should retrieve the date in the previous week that corresponds most closely to the specified date.
fromdate = prevMth(systemDateGet());
need to change the above code so that i could get the date of the previous week instead of previous month with respect to the present date.
No function is needed, just use date arithmetic.
systemDateGet() - 7
This will return the prior week date. This of cause implies you use a calendar with 7 day weeks.
Some additional things you may find useful:
wkOfYr(...)
dayOfWk(...)
DateTimeUtil::[VariousFunctionsHere]
In D365FinOps you can also use:
HcmDateTimeUtil::calculateDateWithOffset(PeriodUnit::Day,7,false)
I would like to make a function that input a NSDate and output the last date of the month. Do you guys know how to do it in Cocoa?
It's the day before the first day of the next month. Add a month, set the day to 1, subtract a day. For setting the day to 1, you'll find it easier to go via NSCalendar. See here for details.
If you're doing date computations, you should always use NSCalendar and related classes, because that's the only way to be forward-compatible with changes to calendars, support for non-"standard" calendars, and so on.
Read the Date and Time Programming Guide section on Calendrical Calculations to get an idea of how these classes work together.
I've already got this data, which was pretty simple:
NSInteger numWeeks = ...;
NSInteger weekdayOfDateA = ...; // i.e. 1 for Sunday
NSInteger weekdayOfDateB = ...; // i.e. 6 for Friday
Just from the logical point of view, I could safely assume that every week in numWeeks has got one Sunday, right?
So numWeeks represents already my number of Sundays. Almost.
But how would I handle the edge cases? i.e. if dateA starts on a Wednesday, and dateB ends on a Monday, that must not neccessarily be a complete set of weeks. It may be like 10 and a half week or something. The problem is: Which part of the interval can be safely ignored since it's "sucked in" already by numWeeks?
I guess NSCalendar & Co. start looking at the first date and simply count up 7 days for each week. So is the only thing I must care of the last tail of that interval?
Think of your weeks as starting e.g. on Monday and ending on Sunday. Determine the number of these weeks you have - you'll have one Sunday for each - in determining this (which you'll probably want to do by rolling e.g. the start forward to the first Monday), if you pass by a Sunday add 1 to the result.
I'm trying to figure out how to calculate store opening times based on the way they are normally expressed. For example: Monday - Friday, 9am - 6pm. I want to know two things from this criteria. Is the store open? If it is how long will it be before it closes?
My confusion with the documentation is that NSDate calculations expect unique date data sets to perform calculations not generic as expressed above. For example NSDate expects 2010-07-12T09:00:00Z & 2010-07-12T18:00:00Z to form the start basis of the calculation and like wise for the days in between.
Do I need to create a data set for the year on this basis to calculate the results? Or is there a clever way I can get NSCalendar / NSDateComponents to do all this hard work for me?
There is no simple/automatic way. You have to parse the string yourself, form two, let's say startDate and endDate (or something alike). From them you can extract the day of the week and time (that's where NSCalendar and NSDateComponents come in handy).