In power automate (desktop), regardless if it is monday or saturday, how can I calculate the tuesday of the current week?
Try this algorithm:
Get current datetime in %CurrentDateTime%
Format %CurrentDateTime% to get the Day of Week with "dddd" format, or use the built in property %CurrentDateTime.DayOfWeek%
Get the number of days to add to %CurrentDateTime% given Day of Week by the following logic:
a) For sunday, 4
b) For monday, 3
c) For tuesday, 2
d) For wednesday, 1
e) For thursday, 0
f) For friday, -1
g) For saturday, -2
Add that number of days to %CurrentDateTime%
Related
I would like to get the following dates in Impala query:
a. Sunday to Saturday Week (SSW):
1. First and Last day of Current week (SSW)
2. First and Last day of Previous week(SSW)
b. Monday to Sunday Week (MSW):
1. First and Last day of Current week (MSW)
2. First and Last day of Previous week(MSW)
c. Month:
1. First and Last Day of Current Month
2. First and Last Day of Previous Month
d. Quarter:
1. First and Last Day of Current Quarter
2. First and Last Day of Previous Quarter
e. Year:
1. First and Last Day of Current Year
2. First and Last Day of Previous Year
This is what I have so far:
SELECT
--Month
date_add(last_day(add_months(current_timestamp(),-1)),1) as Frist_Day_of_Current_Month,
last_day(now()) as Last_Day_of_Current_Month,
date_add(last_day(add_months(current_timestamp(),-2)),1) as Frist_Day_of_Previous_Month,
last_day(add_months(current_timestamp(),-1)) as Last_Day_of_Previous_Month,
--Year
trunc(now(), 'Y') as Frist_Day_of_Current_Year,
date_sub(trunc(now(), 'YEAR'), 1) AS Last_Day_of_Previous_Year
Here are the dates:
SELECT
--SINGLE DAYS
TRUNC(NOW(),'DD') AS TODAY,
ADDDATE(TRUNC(NOW(),'DD'), -1) AS YESTERDAY,
ADDDATE(TRUNC(NOW(),'DD'), -2) AS TWO_DAYS_AGO,
ADDDATE(TRUNC(NOW(),'DD'), -3) AS THREE_DAYS_AGO,
ADDDATE(TRUNC(NOW(),'DD'), -4) AS FOUR_DAYS_AGO,
ADDDATE(TRUNC(NOW(),'DD'), -5) AS FIVE_DAYS_AGO,
ADDDATE(TRUNC(NOW(),'DD'), -6) AS SIX_DAYS_AGO,
ADDDATE(TRUNC(NOW(),'DD'), -7) AS WEEK_AGO,
--WEEK
--SUNDAY TO SATURDAY (NEED TO SCHEDULE THE REPORT TO RUN ON ONLY ON MONDAYS)
TRUNC(NOW(), 'D') - INTERVAL 1 DAY AS FIRST_DAY_OF_CURRENT_WEEK_SS,
TRUNC(NOW(), 'D') + INTERVAL 5 DAY AS LAST_DAY_OF_CURRENT_WEEK_SS,
TRUNC(NOW(), 'D') - INTERVAL 8 DAY AS FIRST_DAY_OF_PREVIOUS_WEEK_SS,
TRUNC(NOW(), 'D') - INTERVAL 2 DAY AS LAST_DAY_OF_PREVIOUS_WEEK_SS,
--MONDAY TO SUNDAY (NEED TO SCHEDULE THE REPORT TO RUN ONLY ON MONDAYS)
TRUNC(NOW(),'DY') AS FRIST_DAY_OF_CURRENT_WEEK_MS,
TRUNC(NOW(),'DY') + INTERVAL 6 DAY AS LAST_DAY_OF_CURRENT_WEEK_MS,
TRUNC(NOW(),'DY') - INTERVAL 7 DAY AS FIRST_DAY_OF_PREVIOUS_WEEK_MS,
TRUNC(NOW(),'DY') - INTERVAL 1 DAY AS LAST_DAY_OF_PREVIOUS_WEEK_MS,
--MONTH
DATE_ADD(LAST_DAY(ADD_MONTHS(CURRENT_TIMESTAMP(),-1)),1) AS FRIST_DAY_OF_CURRENT_MONTH,
LAST_DAY(NOW()) AS LAST_DAY_OF_CURRENT_MONTH,
DATE_ADD(LAST_DAY(ADD_MONTHS(CURRENT_TIMESTAMP(),-2)),1) AS FRIST_DAY_OF_PREVIOUS_MONTH,
LAST_DAY(ADD_MONTHS(CURRENT_TIMESTAMP(),-1)) AS LAST_DAY_OF_PREVIOUS_MONTH,
--QUARTER
TRUNC(NOW(), 'Q') AS FIRST_DAY_OF_CURRENT_QUARTER,
TRUNC(NOW(), 'Q')+ INTERVAL 3 MONTHS - INTERVAL 1 DAY AS LAST_DAY_OF_CURRENT_QUARTER,
TRUNC(NOW(), 'Q') - INTERVAL 3 MONTHS AS FIRST_DAY_OF_PREVIOUS_QUARTER,
TRUNC(NOW(), 'Q') - INTERVAL 1 DAY AS LAST_DAY_OF_PREVIOUS_QUARTER,
--YEAR
TRUNC(NOW(), 'Y') AS FRIST_DAY_OF_CURRENT_YEAR,
TRUNC(NOW(), 'YEAR') + INTERVAL 1 YEAR - INTERVAL 1 DAY AS LAST_DAY_OF_CURRENT_YEAR,
TRUNC(NOW(), 'YEAR') - INTERVAL 1 YEAR AS FRIST_DAY_OF_PREVIOUS_YEAR,
TRUNC(NOW(), 'Y') - INTERVAL 1 DAY AS LAST_DAY_OF_PREVIOUS_YEAR
Thanks,
Regards,
Ahmed
Here I share the query I was looking for :)
TRUNC(add_months(NOW() ,-3), 'Q') - INTERVAL 1 DAY AS LAST_DAY_OF_PREVIOUS_QUARTER_-2
After calculating which day of the week the 1st of January falls on using Gauss's algorithm, as well as calculating the ordinal date for a given calendar date, how can the day of the week of the latter date be calculated?
For example, Gauss's algorithm can tell us that, this year, the 1st of January fell on a Sunday, the 7th day of the week. Today is the 22nd of October, with an ordinal day of 295. How can this information be used to calculate that today is a Sunday?
For common years (= non-leap years), 1st of January and 1st of October are on the same day of the week:
Jan 31
Feb 28
Mar 31
Apr 30
May 31
Jun 30
Jul 31
Aug 30
Sep 31
Sum 273 = 39 x 7
See Wikipedia
22nd October is exactly three weeks later than 1st of October.
An approach I've found, which I haven't tested extensively, but seems to work with the dates I've thrown at it, is...
(ordinal day + day of 1st of January - 1) % 7
Where Mon = 1, Tue = 2,..., Sat = 6, Sun = 0.
In the example mentioned in the question:
(295 + 0 - 1) % 7 = 0 (Sunday)
What is the significance of Go's time.Format(layout string) reference time, ie:
Mon Jan 2 15:04:05 -0700 MST 2006
This specific time couldn't have been chosen completely randomly, right?
Source: http://golang.org/pkg/time/#Time.Format
Each part of the date is used as an index:
Jan -> 1 -> Month
2 -> 2 -> Day-of-Month
15 = 3PM -> 15/3 -> hour
04 -> 4 -> minute
05 -> 5 -> second
2006 -> 6 -> year
-0700 -> 7 -> time-zone
So according to the doc:
Since MST is GMT-0700, the reference time can be thought of as
01/02 03:04:05PM '06 -0700
This makes it easy for the time.Format method to parse human-readable date format specifications that are visually identical to the desired result.
Compare this to for example the strftime C function that uses hard-to-remember format strings such as "%a, %d %b %y %T %z" which represents a RFC 822-compliant date format.
The Go equivalent is: "Mon, 02 Jan 06 15:04 MST".
The time.Format will tokenize this string and analyze each word.
Mon is recognized litteraly as monday so this is the week day's name
the comma is left untouched
02 is recognized as the integer value 2, representing a day-of-month in the index
Jan is the known english abbreviation for the january month, so this is used for the month part
06 is 6 so this the year part
15 is equivalent to 3 and represent the hour
the ':' character is left untouched
04 is 4, therefore the minute
MST is interpreted litteraly
See https://github.com/golang/go/blob/go1.15/src/time/format.go#L151 for the exact algorithm.
In American date format, it's Mon, 1/2 03:04:05 PM 2006 -0700.
1, 2, 3, 4, 5, 6, 7.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have deviced a procedure to find nth working day without using loops.
Please bring around your suggesstions over this -
Algorithm to manipulate working days -
Problem: Find the date of nth working day from any particular day.
Solution:
Normalize to closest Monday -
If today(or the initial day) happens to be something other than monday, bring the day to the closest monday by simple addition or subtraction.
eg: Initial Day - 17, Oct. This happens to be wednesday. So normalize this no monday by going 2 dates down.
Now name this 2 dates, the initial normalization factor.
Add the number of working days + week ends that fall in these weeks.
eg: to add 10 working days, we need to add 12 days. Since 10 days has 1 week that includes only 1 saturday and 1 sunday.
this is because, we are normalizing to nearest monday.
Amortizing back -
Now from the end date add the initial normalization factor (for negative initial normalization) and another constant factor (say, k).
Or add 1 if the initial normalization is obtained from a Friday, which happens to be +3.
If start date falls on Saturday and sunday , treat as monday. so no amortization required at this step.
eg: Say if initial normalization is from wednesday, the intial normalization factor is -2. Hence add 2 to the end date and a constant k.
The constant k is either 2 or 0.
Constant definition -
If initial normalization factor is -3, then add 2 to the resulting date if the day before amortization is (wed,thu,fri)
If initial normalization factor is -2, then add 2 to the resulting date if the day before amortization is (thu,fri)
If initial normalization factor is -1, then add 2 to the resulting date if the day before amortization is (fri)
Example -
Find the 15th working day from Oct,17 (wednesday).
Step 1 -
initial normalization = -2
now start date is Oct,15 (monday).
Step 2 -
add 15 working days -
15 days => 2 weeks
weekends = 2 (2 sat, 2 sun)
so add 15 + 4 = 19 days to Oct, 15 monday.
end_date = 2, nov, Friday
Step 3a -
end_date = end_date + initial normalization = 4, nov sunday
Step 3b -
end_date = end_date + constant_factor = 4, nov, sunday + 2 = 6, nov (Tuesday)
Cross Verfication -
Add 15th working day to Oct, 17 wednesday
Oct,17 + 3 (Oct 17,18,19) + 5 (Oct 22-26) + 5 (Oct 29 - Nov 2) + 2 (Nov 5, Nov 6)
Now the answer is 6, Nov, Tuesday.
I have verified with a few cases. Please share your suggesstions.
Larsen.
To start with, its a nice algorithm, i have doubts about boundary conditions though: for example, what if i need to find the 0th working day from today's date:
Step 1 -
initial normalization = -2 now start date is Oct,15 (monday).
Step 2 -
add 0 working days -
0 days => 0 weeks
weekends = 0
so add 0 + 0 = 0 days to Oct, 15 monday.
end_date = 15, oct, monday
Step 3a -
end_date = end_date + initial normalization = 17, oct wednesday
Step 3b -
end_date = end_date + constant_factor = 17, Oct wednesday or 19,oct friday based on whether constant factor is 0 or 2 as it be only one of these values.
Now lets repeat the steps for finding the 1st working day from today:
Step 1 -
initial normalization = -2 now start date is Oct,15 (monday).
Step 2 -
add 1 working days -
1 days => 0 weeks
weekends = 0
so add 1 + 0 = 1 days to Oct, 15 monday.
end_date = 15, oct, monday
Step 3a -
end_date = end_date + initial normalization = 17, oct wednesday
Step 3b -
end_date = end_date + constant_factor = 17, Oct wednesday or 19,oct friday based on whether constant factor is 0 or 2 as it be only one of these values.
Did you notice, algorithm gives the same end result for 0 and 1. May be thats not an issue if t defined beforehand that 0 working days and 1 working days are considered as same scenario, but ideally they should be giving different results.
I would also suggest you to consider the negative test cases, like what if i need to find -6th working day from today, will your alforithm give me a date in past rightfully?
Lets consider 0th working day from today (17/10, wed).
Step 1 -
start_date = 17/10 wed
normalized date = 15/10 mon
Step 2 -
end_date = normalized date + working days
= 15/10 mon + 0 = 15/10 mon
Step 3 -
amortized_back = end_date_before_amortization + normalization factor
= 15/10 + (+2) = 17/10 wed
since the end_date_before_amortization falls on monday and initial normalization is 2, constant factor = 0.
hence, end_date = 17/10 wed.
now case 2, 1st working day from today.
Step 1 -
start_date = 17/10 wed
normalized date = 15/10 mon
Step 2 -
end_date = normalized date + working days
= 15/10 mon + 1 = 16/10 tue
Step 3 -
amortized_back = end_date_before_amortization + normalization factor
= 16/10 + (+2) = 18/10 thu.
since the end_date_before_amortization falls on tuesday and initial normalization is 2, constant factor = 0.
hence, end_date = 18/10 thu.
Looks to be working for 0th and 1st WD.
I'm looking for the cleverest algorithm for determining the number of fortnightly occurring events in a given calendar month, within a specific series.
i.e. Given the series is 'Every 2nd Thursday from 7 October 2010' the "events" are falling on (7 Oct 2010, 21 Oct, 4 Nov, 18 Nov, 2 Dec, 16 Dec, 30 Dec, ...)
So what I am after is a function
function(seriesDefinition, month) -> integer
where:
- seriesDefinition is some date that is a valid date in the series,
- month indicates a month and a year
such that it accurately yeilds: numberFortnightlyEventsInSeriesThatFallInCalendarMonth
Examples:
NumberFortnightlyEventsInMonth('7 Oct 2010, 'Oct 2010') -> 2
NumberFortnightlyEventsInMonth('7 Oct 2010, 'Nov2010') -> 2
NumberFortnightlyEventsInMonth('7 Oct 2010, 'Dec 2010') -> 3
Note that October has 2 events, November has 2 events, but December has 3 events.
Psuedocode preferred.
I don't want to rely on lookup tables or web service calls or any other external resources other than potentially universal libraries. For example, I think we can safely assume that most programming languages will have some date manipulation functions available.
There is no "clever" algorithm when handling dates, there is only the tedious one. That is, you have to specifically list how many days are in each month, handle leap years (every four years, except every 100 years, except every 400 years), etc.
Well, for the algorithm you are talking about the usual solution is to calculate the day number starting from some fixed date. (Number of day plus cumulated number of days in prev months plus number of years * 365 minus (number of year / 4) plus (number of year / 100) minus (number of year / 400))
Having this, you can easily implement what you need to. You need to calculate which day of week was the 1 January 1. Then you can easily see what is the number of "every second thursdays" from that day to 1 Oct 2010 and 1 Dec 2010. their difference is the value you are looking for.
My solution ...
Public Function NumberFortnightlyEventsInMonth(seriesDefinition As Date, month As String) As Integer
Dim monthBeginDate As Date
monthBeginDate = DateValue("1 " + month)
Dim lastDateOfMonth As Date
lastDateOfMonth = DateAdd("d", -1, DateAdd("m", 1, monthBeginDate))
' Step 1 - How many days between seriesDefinition and the 1st of [month]
Dim daysToMonthBegin As Integer
daysToMonthBegin = DateDiff("d", seriesDefinition, monthBeginDate)
' Step 2 - How many fortnights (14 days) fit into the number from Step 1? Round up to the nearest whole number.
Dim numberFortnightsToFirstOccurenceOfSeriesInMonth As Integer
numberFortnightsToFirstOccurenceOfSeriesInMonth = (daysToMonthBegin \ 14) + IIf(daysToMonthBegin Mod 14 > 0, 1, 0)
' Step 3 - The date of the first date of this series inside that month is seriesDefinition + the number of fortnights from Step 2
Dim firstDateOfSeriesInMonth As Date
firstDateOfSeriesInMonth = DateAdd("d", (14 * numberFortnightsToFirstOccurenceOfSeriesInMonth), seriesDefinition)
' Step 4 - How many fortnights fit between the date from Step 3 and the last date of the [month]?
NumberFortnightlyEventsInMonth = 1 + (DateDiff("d", firstDateOfSeriesInMonth, lastDateOfMonth) \ 14)
End Function