how to set SimpleDateFormat W use Monday as the first day of the week instead of Sunday (US default) - apache-spark

how to set SimpleDateFormat "W" use Monday as the first day of the week instead of Sunday (US default)
I'm trying to extract the week of the month number with F.date_format("dates", "W")
but this would make an observation on the Sunday the first of week 2 instead of the last of week 1
The issue seems to be because simple date format isn't localised and defaults to us/default
Is there a way to set the locale to en/gb for simpleDateFormat to not treat use US defaults
https://issues.apache.org/jira/browse/SPARK-31879

Related

how to calculate the date from the year, week-of-year and day-of-week in EXCEL?

I have the Year, Week-of-Year and Day-of-the-Week as follows:
Year = 2022 (A2) ; Week Year = 35 (B2); Week Day = 4 or Thursday (C2)
and I would like to estimate the Date as dd.mm.yyyy, which is highlighted in yellow as it shows in the EXCEL picture.
I tried many formulas, but I am sure there might be an easy one.
I think you are counting the weeks starting from zero because for 9/1/2022 (YYYY/MM/DD format) the corresponding week is 36 as per the result of function WEEKNUM(DATE(2022,9,1)). In order to use the logic to multiply the number of weeks by 7. You need to use as a reference the first day of the year, if it was a Sunday, if not then go back to the previous Sunday, so you can count the entire week. Bottom line use as a reference date, the Sunday of the first week of the year, not the first day of the year (YYYY/1/1)
Here is the approach we use in cell E2:
=LET(y, A2:A6, wk, B2:B6, wDay, C2:C6, fDay, DATE(y,1,1), seq, SEQUENCE(7),
fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2)) + 7*wk
+ XLOOKUP(wDay, TEXT(seq,"dddd"), seq-1))
We use the LET function to avoid repeating the same calculation. The following expression finds the previous Sunday if the first day of the year (fDay) was not a Sunday:
fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2))
The XLOOKUP function is used to get the numeric representation of the weekday and use the TEXT function to generate the weekdays in a long format. Since we count the entire week, if the weekday is a Sunday (column C in my screenshot), then we don't need to add any day to our reference date, that is why we use seq-1.
Here is the output for several sample data. Assuming the week count starts with zero, if not the formula needs to be adjusted as also the input data.
Notice that the year 2021 started on a Friday, so if we want to find a day for the first week (0) before Friday it will return a date from the previous year. Like in the case of Monday. If you want an error message instead, then the formula can be modified as follow:
=LET(y, A2:A6, wk, B2:B6, wDay, C2:C6, fDay, DATE(y,1,1), seq, SEQUENCE(7),
result, fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2)) + 7*wk
+ XLOOKUP(wDay, TEXT(seq,"dddd"), seq-1),
IF(YEAR(result) <> y, "ERROR: Date from previous year", result))
I found the solution:
Year = 2022 (A2) ; Week Year = 35 (B2); Week Day = 4 or Thursday (C2)
=DATE (A2,1,3)-WEEKDAY(DATE(A2,1,3)) + 7 * B2 + C2 - 6
I found this solution, but you need to do further testing if it really works.
I calculate month from week: =+MONTH(DATE(YEAR(A2);1;1)+B2*7-1)
I calculate week day number from week day name: =MATCH(D2;{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"};0)
And then make date using: =DATE(A2;C2;E2)

moment.js how to set startOf week to monday on current week?

I want to make the date of startOf("week") its start from Monday on current week
Is it possible for make this? My code:
moment().startOf("week").toDate();
This startOf start from saturday proof startOf start from saturday
Take a look at the First Day of Week and First Week of Year section
From the docs (emphasis is mine)
Locale#week.dow should be an integer representing the first day of the week, 0 is Sunday, 1 is Monday, ..., 6 is Saturday.
So just by running this code before doing any calls to moment
// From 2.12.0 onward
moment.updateLocale('en', {
week : {
dow : 1
}
});
It will set the first day of the week to Monday.
After that calling moment().startOf("week").toDate() will give you a Monday.

Change start of weekday from monday to sunday in datetimeindex in python

The default start of the weekday is Monday (0) in Pandas. Am trying to make this as Sunday instead on datetimeindex for getting weekly counts ending on Saturday to forecast a timeseries model.
I got a breakthrough, yes.
From my previous code, I redefined W-SAT instead of the default W.
Previous code:
weekreq = daysreq.resample('W').apply({'Numbers':'sum'})
to
New code:
weekreq = daysreq.resample('W-SAT').apply({'Numbers':'sum'})

How to get / output all days in the current week in Automation Anywhere?

I'm attempting to output all days within the current week. e.g. for this week, show all days, 05/12/2019 through 05/18/2019 only. when the bot is executed next week, only show days 05/19/2019 through 05/25/2019. My current logic outputs the days for this week, but come tomorrow, the dates for this week will be thrown off. Please see the following
...could I get some help with this please?
Using VBS
I would do this using a VBS script, using Run Script command.
The default week start is Sunday you can change it check: https://www.w3schools.com/asp/func_weekday.asp
Pass the day you that you want as a parameter from 0 to 6, and get the data as a return value.
DayNumber: 0 = Sunday ..... 6 = Saturday
InputDate = Date
DayNumber = WScript.Arguments.Item(0)
Result = DateAdd("d", DayNumber - WeekDay(InputDate, 2), InputDate)
WScript.StdOut.Write(Result)
'MsgBox(Result)
Using MetaBot
Metabot Link: Change Date and Time Format
You will have to run the following logic in sequence.
Input: DayNumber: 0 = Sunday ..... 6 = Saturday
Using DayOfWeek Logic, Get the Day of the week and assign it to
WeekDay variable, it will return the name, not the number, and the input will be Date.
Using IF conditions convert the name of
the day to number, start from 0 to 6 as your first day in the week,
which is Sunday, and using variable operation assigns the value to
NumWeekDay variable.
Using variable operation, Get the offset by subtracting DayNumber, the day you want minus NumWeekDay,
and assign the value to Offset variable.
Using AddDays, Input
the date and the offset, and you will get the date of the day that you want.

Getting a day date for last week from a given date

I know how to get the last Saturday date using date --date="sat. last week" +"%m%d%Y" However, I am unable to get the last Saturday date for a particular given date.
I am trying to find the date of last saturday date from a given date. For an instance, let us say that the given date is 20140605 (in YYYYMMDD format) or any weekday date from that particular week. This given date could be any weekday date (20140602-20140606) in the following week for that Saturday, I need to derive the Saturday date for the week before this date which would be (in my case)= 20140531. How can I achieve this?
I just barfed a bit on my keyboard, but this seems to work:
D="20140605"; date --date "$D $[($(date --date "$D" +%u) + 1) % 7] days ago" +"%Y%m%d"
I have a small Python snippet for you:
from datetime import datetime, timedelta
import sys
fmt = "%Y%m%d"
daystring = sys.argv[1]
day = datetime.strptime(daystring, fmt)
for d in range(7):
# Go back a few days, but not more than 6.
testdate = day - timedelta(days=d)
# If the test date is a saturday (0 is monday, 6 sunday), output that date.
if testdate.weekday() == 5:
print testdate.strftime(fmt)
Test:
$ python test.py 20140605
20140531

Resources