Week number to week commencing date USING PYTHON? - python

n=10
I want to get the week commencing date of the 10th week of the current year (2022), i.e., 3/7/2022. How can this be done using datetime functions?

You need to use the %W directive, but you also need to specify what the start day is of the week and of course the year.
Example:
from datetime import datetime
print(datetime.strptime("10-2022-1", "%W-%Y-%w"))
Result:
2022-03-07 00:00:00
10 is the week number
2022 is the year
1 is the day of the week to start with (Monday) in order to get an actual date.
Datetime formats: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

Related

How to get last Friday's date with Python?

Is there a way to use Python to get last Friday's date and store it in a variable, regardless of which day of the week the program runs?
That is, if I run the program on Monday June 19th 2021 or Thursday June 22nd 2021, it always returns the previus Friday as a date variable: 2021-07-16.
To get the day of the week as an int we use datetime.datetime.today().weekday() and to subtract days from a datetime we use datetime.today() - timedelta(days=days_to_subtract) now we can make a dictionary linking the day of the week to the number of days to subtract and use that dictionary to make a subtraction:
from datetime import datetime, timedelta
d = {0:3,1:4,2:5,3:6,4:0,5:1,6:2}
lastfriday = datetime.today()-timedelta(days=d[datetime.today().weekday()])
Your question is similar to this one
Here's a starting point:
import datetime
def get_last_friday():
current_time = datetime.datetime.now()
last_friday = (current_time.date()
- datetime.timedelta(days=current_time.weekday())
+ datetime.timedelta(days=4, weeks=-1))
return last_friday

Python: get first date of the week from calender week and year (working for 2020 but not for 2021)

I would like to extract the first date of the week from the year and the calender week (in Europe where the first calender week is the week that includes the 4th of January)
My code works correctly for
import datetime
year=20
week=53
print(datetime.datetime.strptime(str(year) + "-"+ str(week-1) +'-1-CET', "%y-%U-%w-%Z"))
the output is 2020-12-28 00:00:00 which is correct
for
import datetime
year=21
week=1
print(datetime.datetime.strptime(str(year) + "-"+ str(week-1) +'-1-CET', "%y-%U-%w-%Z"))
I get also 2020-12-28 00:00:00.The correct output would be 2021-01-04.
Could you please tell me where my mistake is?
Thanks
Using %V instead of %U should make things easier.

Django: Localize Date without Year

Any idea how to localize a date, which should only displays day and month respectively month and day?
I know how to format the whole date:
formats.date_format(datetime.now(), format="DATE_FORMAT", use_l10n=True)
Which returns the date as: Feb. 6, 2020 or 6 Feb. 2020 according to the locale setting.
I need the same Output, but without the year.
You can use MONTH_DAY_FORMAT.
formats.date_format(datetime.now(), format="MONTH_DAY_FORMAT", use_l10n=True)

Get first and last day of given week number in python

I need a function which returns the first and last day respectively the Monday and Sunday of a given week number (and a year).
There is a difficulty for weeks where Jan 1st is not a Monday so I cannot use the standard datetime.datetime.strptime().
Here's the solution:
import calendar
import datetime
from datetime import timedelta
def get_start_and_end_date_from_calendar_week(year, calendar_week):
monday = datetime.datetime.strptime(f'{year}-{calendar_week}-1', "%Y-%W-%w").date()
return monday, monday + datetime.timedelta(days=6.9)
I extended the great logic of this post.
Update
There is a pitfall with the first calendar week. Certain countries handle the first week number differently. For example in Germany, if the first week in January has less than 4 days, it is counted as the last week of the year before. There's an overview at Wikipedia.
This other version found here is flawless and simple: https://www.pythonprogramming.in/how-to-get-start-and-end-of-week-data-from-a-given-date.html
##
# Python's program to get start and end of week
from datetime import datetime, timedelta
date_str = '2018-01-14'
date_obj = datetime.strptime(date_str, '%Y-%m-%d')
start_of_week = date_obj - timedelta(days=date_obj.weekday()) # Monday
end_of_week = start_of_week + timedelta(days=6) # Sunday
print(start_of_week)
print(end_of_week)

How to determine the date of Starting day of current and last week in python ?

I need to find the date of the starter day of current and last week.
Here, business day of a week starts from SUNDAY. So, for today the date I want is to be "07-16-2017" as "mm--dd--yyyy" format. I can get today's date easily from datetime.datetime.now().strftime ("%Y-%m-%d") but from the sysdate I have to pull out the starter day of the week.
I need the starter day's date for last week as well.
There is no default method for week information in datetime. Is there any other method in any package in python to determine the required information ?
You can use calendar.firstweekday() to check what the first day of the week is on that computer (0 is Monday, 6 is Sunday).
1) Let's say that firstweekday returned 1 (Sunday). Then you can use
date.today() - datetime.timedelta(days=date.today().isoweekday() % 7)
to compute the date of the last Sunday.
2) Let's say that firstweekday returned 0 (Monday).
date.today() - datetime.timedelta(days=date.today().isoweekday() % 7 - 1)
to compute the date of the last Monday.
Hope this gives you some direction.

Categories

Resources