Formula to add days to a Gregorian date - date

I was looking at Tomohiko Sakamoto's weekday calculator. It's a formula to calculate the day-of-week directly given year, month, day. That made me wonder what other neat date calculation shortcuts exist.
In particular, given an input date as (in_year, in_month, in_day) and a number of days N to add, what's a formula for returning the output (out_year, out_month, out_day)? Is there a well-known trick like the algorithm above?
One way would be to convert the input to a Julian day (a count of days since 4713 BC), add N to it, and then convert back. There are formulas for conversion in both directions. But the combined formula would be quite unwieldy. Is there a simplified version?
Perhaps there is even a formula to move forward or back by a certain number of weekdays.
This question isn't "how do I do date arithmetic in my favourite programming language?" I know how to call the date library to perform these operations. It's more curiosity and the hope of starting a collection of cool date algorithms.

Some of the answers in Algorithm to add or subtract days from a date? will be relevant to this. In particular http://howardhinnant.github.io/date_algorithms.html gives code to convert y,m,d to a count of days and back again. Those two routines run back to back would be pretty fast.

Related

How is it possible to calculate value for the same day last week in tableau for every day?

I would like to calculate values for yesterday for every date in a table.
Firstly I have simply calculated the number of orders for each date using COUNTD function.
But afterwards I encountered some problems trying to calculate values for "yesterdays".
Please refer to the image
example
For example for 12th of April I would like to obtain the value for 11th of April.
On the internet there are a lot of examples using today() or max() or table functions but they do not give the required result because a would like to filter for example 12th April but still see the value for 11 April.
Could you please help, how is it possible to do this?

Power BI - Calculating a price/cost until a variable end date

Let's say I have a table of projects/programs/subscriptions/etc, it doesn't really matter as long as there is a price or cost per some amount of time. My table includes at least the following columns:
[ProjectName]
[StartDate]
[EndDate]
[CostPerDay]
I'm trying to allow the user to choose another date (slicer I assume?) and display the cost of each and all projects up to that date. Is this possible?
Edit: After the first responses I realize the original question was very poorly worded. Sorry about that. I've reworded it and I'll explain more here:
I am not trying to filter the programs by end date. I'm trying to sum a cost up until the end date OR slicer date, whichever is earlier.
Here's a short example table:
So we can also think of it as a Gantt chart like this:
Now imagine sliding a vertical line along that chart. I want to see the total cost up to that date.
I'm sure it will have to do with counting days between start date and the slicer date, then multiplying by cost. But how do we not include days after the end date of each project? Or it may be easier to do a range slicer with a min and max date, but again not counting days before or after each project.
To word it differently: can I input a date range, count the days that each project has in common with that range, and (the simple part) multiply days by cost?
It looks like what you need is a table visualization with a slicer for End date. Once the table and slicer are created, you can click on the small down facing arrow in the slicer and choose "Before". If you want both start date and end date in the equation, then you would have to add one more slicer for "Start date". If this is not what you are looking for kindly provide additional details so that we can help you out.

Flatline: How to calculate days between two dates

I want to add a calculated field in a BigML datasheet with the result of days between two dates.
I'm trying to figure out how to calculate the number of days between two date fields with Flatline language but I don't know how to do it even reading the doc.
Any clue about how create this calculated field?
PS: Somebody with enough reputation could create and add tags "bigml" and "flatline"?
Currently, the only way to subtract dates is by first transforming them to an
epoch (number of milliseconds since 1970) and then computing the difference:
(- (epoch "12/03/1990") (epoch "01/01/1988"))
That will give you the number of milliseconds between the two dates, which then
can be transformed to other units. What that won't give you is of course the
difference in calendar days: we don't have yet in Flatline a way of subtracting
calendar dates. But it shouldn't be too difficult to add them if it's a feature
you need :)

Using Apple Numbers, is there a way to display the difference between two dates in years, with decimals?

I am simply trying to display the difference between to dates, but have the result contain a decimal. For example, for the two dates 8/1/2009 and 7/6/2010, I would like a return of about "0.9" or something like that. I can get the 339 days result by simply subtracting one cell from another, but when I use "Y", it only gives whole numbers. Of course the date format boxes under the Cell section of the format and options are there, but they infuriatingly stop at "Week" and don't offer months or years. I don't want, for example, "10 months, 25 days". I'd really like it to simply be years with decimals, like "1.5" years instead of x amount of weeks or months. Any help appreciated..thanks.
You're after the YEARFRAC function:
Finds the fraction of a year represented by the number of whole days between two dates
Alternatives can also be found in the DATEDIF function, which allows you to return the result in a number of different units, but I'll let you read around that for yourself.

SPSS Modeler - Date Function fpr lastweek

I wanted to know if it was possible to use a date function for last week's date ranges. Currently I'm using the method as below.
Thanks.
datetime_date('Sale.Date') >= datetime_date(2016,04,18)
and
datetime_date('Sale.Date') <= datetime_date(2016,04,24)
Not a great solution but:
--datetime_date(datetime_year(#TODAY), datetime_month(#TODAY), datetime_day(#TODAY)-7)
This will work for all but 7 days of the month.
You could also get a rough estimate by calculating the following (note that you will use the method I outline and not the equations. you must copy paste the equations into their designated locations).
--a=date_in_years(#TODAY)-7/365
--year=a-(a mod 1)+1900
--month=(a mod 1)-((a mod 1) mod 1/12) *12
etc. then entering these formulas into this formula:
--datetime_date(year,month,day)
OR you can use SQL to calculate a 7 day difference. This will be the accurate and easy but would require the correct setup.
It is a little unclear what you are looking for exactly; what do you mean by "last week"? Is that the 7 days prior to today or is it the last calendar week? If it's the calendar week, which definition are you using? What day of the week does a week start? When does the first week of the year start?
Unfortunately, these definition vary between different parts of the world and even for different uses within countries.
Dates in SPSS Modeler are all represented as the number of seconds from Jan. 1st, 1900, so if you are looking for the dates of the past 7 days, the calculations are fairly trivial as you can use the datetime_in_seconds() function to get the numeric representation of any date or timestamp.
If you are looking for calendar weeks, things get a little more complicated, but I should be able to help with that, too, if you can answer the above questions.
For SPSS Modeler you can use a Derive Node and I see two options to do that:
A)
Derive Node: date_weeks_difference(date1,date2)
And then use a Filter node to keep only the derive node = 1
B)
Or you can use a function inside the Derive Node, creating a dummy variable:
if date_weeks_difference(date1,date2)= 1 then 1 else 0 endif