Find monday of current week in Python [duplicate] - python

This question already has answers here:
Getting the date of the first day of the week
(2 answers)
Python: give start and end of week data from a given date
(5 answers)
Closed 3 years ago.
I am trying to get the timestamp of monday at 00:00 of the current week in python. I know that for a specific date, the timestamp can be found using
baseTime = int(datetime.datetime.timestamp(datetime.datetime(2020,1,1)))
However, I want my program to automatically find out, based on the date, which date monday of the current week was, and then get the timestamp. That is to say, it would return different dates this week and next week, meaning different timestamps.
I know that the current date can be found using
import datetime
today = datetime.date.today()
Thanks in advance

I am trying to get the timestamp of monday at 00:00 of the current week in python
You could use timedelta method from datetime package.
from datetime import datetime, timedelta
now = datetime.now()
monday = now - timedelta(days = now.weekday())
print(monday)
Output
2020-01-27 08:47:01

Related

How do I take end date as 11 months ahead of start date in the datetime format in Python?

I am taking start date as user input in the datetime format in python. Using the start date, I want to take end date as 11 months from start date.
start_date= month_dt
end_date =
Here, month_dt is taken in taken as user input in the datetime format, e.g. 2020-01-01 . How do I take end_date as 11 months ahead of the start date?
import datetime
from dateutil.relativedelta import relativedelta
print(datetime.date.today() - relativedelta(months=+11))
Current date reduced by 11 months

Day of the week pyspark

I'm trying to get the day of the week from a given date in pyspark could anyone help me ?
dt = dt.withColumn("week_day_number", date_format(col("TRANSACTION_DATE"), "u"))
I'm trying to get the day of the week from a given date ..
See here.
(Once you have a datetime instance just call weekday())
Monday is 0 and Sunday is 6
import datetime
print(datetime.datetime.today().weekday())

Work out if date is in current week python

I'm trying to write a bit of code to check if a document has been updated this week, and if not to read in the data and update it. I need to be able to check if the last modified date/time of the document occurred in this week or not (Monday-Sunday).
I know this code gives me the last modified time of the file as a float of secconds since the epoch:
os.path.getmtime('path')
And I know I can use time.ctime to get that as a string date:
time.ctime(os.path.getmtime('path'))
But I'm not sure how to check if that date was in the current week. I also don't know if its easier to convert to a datetime object rather than ctime for this?
you can use datetime.isocalendar and compare the week attribute, basicallly
import os
from datetime import datetime
t_file = datetime.fromtimestamp(os.path.getmtime(filepath))
t_now = datetime.now()
print(t_file.isocalendar().week == t_now.isocalendar().week)
# or print(t_file.isocalendar()[1]== t_now.isocalendar()[1])
# to compare the year as well, use e.g.
print(t_file.isocalendar()[:2] == t_now.isocalendar()[:2])
The ISO year consists of 52 or 53 full weeks, and where a week starts on a Monday and ends on a Sunday. The first week of an ISO year is the first (Gregorian) calendar week of a year containing a Thursday. This is called week number 1, and the ISO year of that Thursday is the same as its Gregorian year.

Python - Calendar / Date Library for Arithmetic Date Operations

This is for Python:
I need a library that is able to do arithmetic operations on dates while taking into account the duration of a month and or year.
For example, say I add a value of "1 day" to 3/31/2020, the result of should return:
1 + 3/31/2020 = 4/1/2020.
I also would need to be able to convert this to datetime format, and extract day, year and month.
Does a library like this exist?
import datetime
tday = datetime.date.today() # create today
print("Today:", tday)
""" create one week time duration """
oneWeek = datetime.timedelta(days=7)
""" create 1 day and 1440 minutes of time duraiton """
eightDays = datetime.timedelta(days=7, minutes=1440)
print("A week later than today:", tday + oneWeek) # print today +7 days
And the output to this code snippet is:
Today: 2020-03-25
A week later than today: 2020-04-01
>>>
As you see, it takes month overflows into account and turns March to April. datetime module has lots of things, I don't know all its attributes well and haven't used for a long time. However, I believe you can find nice documentation or tutorials on the web.
You definitely can create any specific date(there should be some constraints though) instead of today by supplying day, month and year info. I just don't remember how to do it.

python get tommorows date dd.mm.yy problem [duplicate]

This question already has answers here:
How to increment a datetime by one day?
(8 answers)
Closed 4 years ago.
So basically I want to check if a certain string includes tommorows date so i made this date variable (see code).
Now the problem is every month on the last day this is going to be wrong. For example the 30th of september, with the way i did it, its going to say that tommorow will be the 31 of september, wich does not exist however. I need it to say that tommorow is the 1. of october.
Any suggestions? It has to be in the format dd.mm.yy please.
day = str(datetime.datetime.today().day+1)
month = str(datetime.datetime.today().month)
year = str(datetime.datetime.today().year)
date = day + "." + month + "." + year
just add one day to today
tomorrow = datetime.datetime.today() + datetime.timedelta(days=1)
print(str(tomorrow),tomorrow.strftime("%d.%m.%y"))
relativedelta can also be used
import datetime
from dateutil.relativedelta import relativedelta
tomorrow = datetime.datetime.today() + relativedelta(days=1)
print(str(tomorrow),tomorrow.strftime("%d.%m.%y"))

Categories