python datetime strptime unexpected output [duplicate] - python-3.x

This question already has answers here:
Get date from week number
(8 answers)
Closed 2 years ago.
datetime.datetime.strptime('2015.18', "%Y.%W")
datetime.datetime(2015, 1, 1, 0, 0)
Whats wrong with my code? 2015 is Year and 18 is the calendar week?
output is completely wrong. Thank you!

You have to add week day as well to get the desired output.
Following line should work, where 1 is the first day of week.
>>> datetime.datetime.strptime('2015.18.1', "%Y.%U.%w")
datetime.datetime(2015, 5, 4, 0, 0)
Here Sunday is assumed as the first day of the week by python because we are using %U. To set Monday as first day of week use %W or refer to documentation here https://docs.python.org/2/library/datetime.html

Related

Current week in Python? [duplicate]

This question already has answers here:
Python: give start and end of week data from a given date
(5 answers)
how to get all dates of week based on week number in python
(7 answers)
Adding days to a date in Python
(16 answers)
Closed 9 months ago.
good morning, I'm in the middle of a project and I'm stuck on finding a way to return every day of the current week in a list. It would be something like this:
week=[ '2022-06-07' , '2022-06-08' , '2022-06-09' , '' , '...']
I would be very grateful if you could explain to me a way to obtain this result,,, thanks
You can shorten this but I wrote it out like this for clarity.
from datetime import datetime, timedelta
date_obj = datetime(2022, 6, 7) # Today, Tuesday
monday = date_obj - timedelta(days=date_obj.weekday()) # assuming start of week is Monday
week = []
for i in range(7):
week.append((monday + timedelta(days=i)).strftime("%Y-%m-%d"))
print(week)
Output:
# Monday 6/6 to Sunday 6/12
['2022-06-06', '2022-06-07', '2022-06-08', '2022-06-09', '2022-06-10', '2022-06-11', '2022-06-12']

What is the purpose of offset in conjunction with datetime in Python 3.x?

I'm new to python and I'm looking to work with datetime. I have some files generated every Sunday and I like to move the furthest Sunday out of the current folder eg: 2020-04-12, 2020-04-19, 2020-04-26.
I have found some examples on getting a specific date from today's date and I was able to modify it a tab bit. Eg. I can go back and get last week's Sunday with a specific date:
from datetime import date
from datetime import timedelta
import datetime
today = datetime.datetime(2020,4,13)
offset = (today.weekday() + 1) % 7
sunday = today - timedelta(days=offset)
#print (offset)
print(sunday)
I am confused by the offset variable. What is (today.weekday() + 1) % 7 doing? I have read the Python doc and not quite wrapping my head around it. With +1, I get the date 2020-04-12, which is a Sunday, great. When I do -1 (the other thing is if I set it to (today.weekday() - 1) % 7), I get 2020-04-07, a Tuesday. How did it jump from Sunday the 12th to Tuesday the 7th?
Additionally, how do I get it to jump back 3 weeks? that's where I'm also stuck.
Alright, so if today's Wednesday, then today.weekday() is 2, because it starts counting from 0 on Monday. Not sure why, but that's life.
So (2 + 1) % 7) = 3. That means that 3 days ago was Sunday. Hence your code:
offset = (today.weekday() + 1) % 7 # How many days ago was sunday
sunday = today - timedelta(days=offset) # Go backwards from today that many days
You'll notice that if you subtract one instead of add one, that means we're going backwards (because we're sutracting the timedelta object) by two fewer days than before (because 2 - 1 is equivalent to (2 + 1) - 2, that is, two fewer days). If you started by going backwards enough days to get to Sunday, and now you're going backwards two fewer days, you'll end up on Tuesday, which is two days later than Sunday.
The easiest way to shift which week you're headed to is to set the weeks argument in timedelta:
n_weeks = 3
sunday = today - timedelta(days=offset, weeks=n_weeks)
that's equivalent to, but much prettier than:
sunday = today - timedelta(days=offset + n_weeks * 7)

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.

numbered weekdays: find the k-th integer

The task I´m given is as follows:
Days of week are numbered as:
0 — Sunday,
1 — Monday,
2 — Tuesday,
...
6 — Saturday.
An integer K in the range 1 to 365 is given. Find the number of the day of the week for the K-th day of the year provided that in this year January 1 was a Thursday.
I´m struggling to create a general and user-friendly code for the given problem. Thanks for any help!
date=int(input('enter the K-th day: '))
if(day==0):
print("Sunday")
if(day==1):
print("Monday")
if(day==2):
print("Tuesday")
if(day==3):
print("Wednesday")
if(day==4):
print("Thursday")
if(day==5):
print("Friday")
if(day==6):
print("Saturday")
We know that day 1 was a Thursday, this means we also know that day 8, 15, 21 and so on are also Thursdays. All these numbers have in common that if you divide them by 7, the remainder is 1.
Day 2 was a Friday, so day 9 and 16 are also Fridays. Divided by 7 the remainder for all Fridays is 2.
If you do this for all weekdays you will notice a pattern ;-) and if you check the python docs you'll stumble across the Modulo operator.
This should point you in the right direction without spoiling the fun. If you need any additional help, just let me know and I'll reify the answer.
here you go!
nday = int(1)
dday=((3+nday)%7)
print(dday)

total number of days from current date if we want to know like after 2 weeks, 3 months , 1 year

I want to know the total number of days from current date.
if given date is 2 Feb 2018. and i want to calculate number of days after 2 weeks/3 months/1 year. keeping in mind of leap year or not.
Language - python3
Is there any library in python which give me this.
The dateutil module has a way to do this very conveniently. Sample code:
import datetime
import dateutil
today = datetime.date.today()
delta = dateutil.relativedelta.relativedelta(years=1, months=3, weeks=2)
desired_date = today + delta
desired_date
Output:
datetime.date(2019, 5, 16)

Resources