How to calculate how many Sundays are between two dates? - iphone

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.

Related

How to calculate the weekend when counting a date range in Google Sheets?

I have the below columns
StartDate EndDate CountDay
01 May 20 05 May 20 ?
As you see, 01 May is Friday, so from 01-05 May if we count all days including weekend it will be 4 days.
What I want is on column "CountDay" it only counts the Workdays, not the weekend.
SO the expected result would be 2.
Anyone know how to do it using a formula in Google Sheets?
Do you consider Fridays as part of the weekend?
If yes, then you could also try the following formula:
=NETWORKDAYS.INTL(A10, B10,"0000111")
If not, please use this formula:
=NETWORKDAYS.INTL(A10, B10)
How the formulas work.
By using the function NETWORKDAYS.INTL we can "adjust" the weekend (non-working weekdays) to our liking.
In this case we account Fridays as our non-working weekdays by using as the 3rd parameter 0000111 instead of the default 0000011 where every 0 represents a working weekday and every 1 a non-working weekday.
(Very useful for people working part-time)
Someone who has part-time work on only Mondays, Wednesdays and Fridays and wants to calculate the working days Friday, 1 May 2020 - Tuesday, 30 June 2020 could adjust the formula to:
=NETWORKDAYS.INTL(A10, B10,"0101011")
As explained on the official Google help page for NETWORKDAYS.INTL
weekend – [ OPTIONAL – 1 by default ] – A number or string representing which days of the week are considered weekends.
String method: Weekends can be specified using seven 0s and 1s, where the first number in the set represents Monday and the last number is for Sunday. A zero means that the day is a work day, a 1 means that the day is a weekend. For example, “0000011” would mean Saturday and Sunday are weekends.
Number method: Instead of using the string method above, a single number can be used. 1 = Saturday/Sunday are weekends, 2 = Sunday/Monday and this pattern repeats until 7 = Friday/Saturday. 11 = Sunday is the only weekend day, 12 = Monday is the only weekend day and this pattern repeats until 17 = Saturday is the only weekend day.
I just found how to do it:
=if(weeknum(A10)<weeknum(B10),B10-A10-2*(weeknum(B10)-weeknum(A10)),B10-A10)
something like that

how to count week number of month using dataprep

I tried to count week number of month using below code,but I got weird num like -48.(I know my logic is weird lol)
Could you point out the fault of below code to make weeknum of month.
I need sensei's help.
I used Dataprep
WEEKNUM(date)-WEEKNUM(DATE(YEAR(date),MONTH(date),1))
no error , but some values are -48,47......
Your logic is mostly sound, except you're only thinking of WEEKNUM in the context of a single year. In order to have non-overlapping weeks, the week containing January 1 is always week 1 (regardless of the period), so in this case December 29–31, 2019 are all going to be week 1, just like the following 4 days of January will be. It makes sense, but only when you think about it in context of multiple years.
You can work around this for your case by checking the WEEKNUM and MONTH values conditionally, and then outputting a different value:
IF(AND(MONTH(date) == 12,WEEKNUM(date) == 1),53,WEEKNUM(date)) - WEEKNUM(DATE(YEAR(date),MONTH(date),1))
While hard-coding the week number of 53 is a little hacky, the whole point of week numbers is to always have 52 subdivisions—so I don't really see any concerns there.
You may also want to add 1 to the resulting formula (unless you want your week numbers to start with 0 each month).

Determine number of days represented by a time range in Java

Using Java 8 time I am simply trying to figure out the number of days represented in a time range. Consider the following:
LocalDate start = LocalDate.of(2016, Month.MARCH, 28);
LocalDate end = LocalDate.of(2016, Month.MARCH, 31);
Period period = Period.between(start, end);
The number of days in period is 3 which represents the number of days between the 2 dates, inclusive of start and exclusive of end. What I want is the number of days represented by the 2 dates which is actually 4 (March 28, March 29, March 30, March 31).
I know I can just add 1 to the number of days returned from Period.between() but I guess I was surprised that I couldn't find another call to return exactly what I want. Am I missing something or is adding 1 the only solution?
tl;dr
Always define your spans of time by the Half-Open approach where:
Beginning is inclusive.
Ending is exclusive.
When you want the four days of March 28, March 29, March 30, March 31, make the beginning March 28 and the ending April 1. Run through those dates starting at the first (March 28th) while going up to, but not including, the last (April 1st).
2016-03-28/2016-04-01
Half-Open
Am I missing something
You may be missing an appreciation for the usefulness of the Half-Open approach in defining spans of time.
Generally, the best practice for defining spans of time is the Half-Open approach. In Half-Open, the beginning is inclusive while the ending is exclusive.
This approach solves the problem of dealing with fractional seconds. Intuitively, many programmers will try to find the last possible moment as the ending of a span of time. But that last moment involves an infinitely divisible last second. You might think, "Well, just go to three decimal place for milliseconds, 12:59.59.999 for the end of noon lunch break, as that is all the resolution I will ever need, and that is the resolution of the legacy java.util.Date class.”. But then you would fail to find matches in your database like Postgres that store date-time values with a resolution of microseconds, 12:59:59.999999. So you decide to use six decimal places of fraction, x.999999. But the start experiencing mismatches with date-time values in the java.time classes, and you learn the offer a resolution of nanoseconds for nine digits of fractional second, x.999999999. You can disembark this carousel of frustrating bugs by using the Half-Open approach where the ending runs up to, but does not include, the next whole second.
I believe you will find consistent use of the Half-Open approach throughout your date-time handling code (whether fractional seconds may be involved or not) will:
Make your code easier to read and comprehend.
Ease the cognitive load overall.Knowing all your spans of time carry the same definition eliminates ambiguity.
Reduce bugs.
Examples:
A noon lunch period starts at the moment the clock strikes noon (12:00:00) and runs up to, but does not include, the moment when the clock strikes one o’clock. That means 12:00:00 to 13:00:00.
A full day starts at the first moment of the day (not always 00:00:00, by the way) and runs up to, but does not include, the first moment of the following day.
A week starts on a Monday and runs up to, but does not include, the following Monday. That means seven days in Monday-Monday.
A month starts of the first of the month and runs up to, but not including, the first of the following month. So the month of March is March 1 to April 1.
LocalDate
Rather than one adding 1 to get a total of days, define your span of time as Half-Open: beginning-is-inclusive, ending-is-exclusive. If you are trying to represent the four dates of March 28, 29, 30, and 31, then I suggest you define a span of time from March 28 to April 1.
LocalDate start = LocalDate.of( 2016, Month.MARCH, 28 ) ; // inclusive
LocalDate stop = LocalDate.of( 2016, Month.APRIL, 1 ) ; // exclusive
Period
The java.time classes wisely use the Half-Open approach. So the Period.between method treats the ending as exclusive, as noted in the Question. I suggest you go-with-the-flow here rather than fight it. Search Stack Overflow for many more examples of how well this approach works.
Period p = Period.between( start , stop );
p.toString(): P4D
ChronoUnit
If you want a total number of days, such as 45 for a month and a half, use the ChronoUnit enum, an implementation of TemporalUnit. See this Question for discussion.
Again, the java.time classes use the Half-Open approach. So
long daysBetween = ChronoUnit.DAYS.between( start, stop );
4
Live code
See this example code run live at IdeOne.com.
There doesn't seem to be an inclusive end date method in either Period or LocalDate so it seems that the only thing to do is something like:
Period.between(start, end.plusDays(1))
or
start.until(end.plusDays(1))
(Period.between just calls LocalDate.until)
I belive your day counting differs from java's. Period.between according to documentation: http://docs.oracle.com/javase/8/docs/api/java/time/Period.html#between-java.time.LocalDate-java.time.LocalDate-
"The start date is included, but the end date is not." Having that in mind - yes, adding 1 is the only solution.
the signature of the method is
between(LocalDate startDateInclusive, LocalDate endDateExclusive) {
and the method is not oerloaded, you have no choice other than add 1 to the given result...

Recurring calendar event on First of the month

I have a recurring calendar event on 1st of each month, when 1st falls on Sat/Sun is it possible for iCal to schedule it for last working day Fri?
If I understood you correctly the following RRULE should do the trick:
FREQ=MONTHLY;BYDAY=1MO,1TU,1WE,1TH,1FR,-1FR;BYMONTHDAY=1,-1,-2
See the next 100 instances
It basically does two things:
Iterate the first weekday that falls on the 1st day of the month
Iterate the last Friday of each month that falls on the last or 2nd last day of the month (in which cases the 1st of the next month falls on Saturday or Sunday).
A slightly shorter version that should yield the same results is
FREQ=MONTHLY;BYDAY=1MO,1TU,1WE,1TH,FR;BYMONTHDAY=1,-1,-2
This just iterates every Friday, but only keeps the ones that fall on the first, last and 2nd last day of each month. Though it looks like the recurrence expansion service above disagrees. That's probably a bug.
I'm assuming you mean "the last working day Friday of the previous month", in which case I don't think it possible. It would mean that some months have 2 occurrences when others have 0, which doesn't really work.
You could easily do the first working day of the month (the 1st, or the first Monday):
FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=1
Or, you could do always the last working day of the month:
FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1

Representing repetitive dates

Repetitive Dates:
Billing cycles come in a lot of different formats, for example: "the first of the month", "third Friday of the month", or "first weekday on or after 21st day after the 13th of the month" (thanks visa!). My goal is to be able to represent these different billing cycles in one easily parsed database text field.
You forgot weekend processing. If my bill is due on a Sunday, I want it to show up on Friday. That kind of thing. I think there are three possibilities:
Always before the weekend
Always after the weekend
If it's Saturday, Friday, and if it's Sunday, Monday
Current solution:
YYYY/MM/DD+YY/MM/DD+DOW
all blocks (delimited by +) are optional as well as all parts of each block.
first block denotes the starting date
if blank - denotes today
if missing portion - denotes all... ie "//12" would denote the 12th of the month every month.
negative denotes counting from the end of the period.. ie "//-1" denotes the last day of the month
overruns are carried into the next month ie "//40" would be 40 days after the first of the month
second block denotes the change in date between repetitions
if blank - set date (from first block)
Add present portions - ie "/1/" means every month after the given starting date
third block denotes the day of the week the calculated date should be moved to
available are [m,t,w,r,f,s,d,b] (7 days of the week, "business day")
Calculation:
Calculation start on the date specified in the first block, filling in the missing portions with the date the record is added.
the DOW is then added to get a valid starting date. This is the first date of the event.
the next date is calculated by adding the second block to the last calculated first block and adding the DOW to get the next date.
Examples:
on march 12th, 2008 - "2008/3/12"
the first of the month every month - "//1+/1/"
third Friday of the month every month - "//21+/1/+f"
first weekday on or after 21st day after the 13th of the month - "//34+/1/+b"
every 2 weeks from today - "+//14"
last day of every second month starting with march - "/3/-1+/2/"