The problem:
I am trying to print out the date from a month ago. So instead of the result being:
>>> 2021-03-12
It will be in this instead
>>> 2021-02-12
Here is my code:
from datetime import date
import datetime
from email.utils import formatdate
now = formatdate(timeval=None, localtime=False, usegmt=True)
tday = date.today()
print(tday)
I have seen tons of different examples but all of them change the format of the date structure that I already have.
from datetime import datetime
from dateutil.relativedelta import relativedelta
now = datetime.now()
last_month_date = now + relativedelta(months=-1)
last_month_date=last_month_date.split(" ")[0]
Use dateutil as it has a improved delta
Add to #chess_lover_6
from datetime import datetime
from dateutil.relativedelta import relativedelta
now = datetime.now()
last_month_date = now + relativedelta(months=-1)
last_month_date.strftime('%Y-%m-%d')
You will get 2021-02-12
Related
I am using the datetime Python module. I am looking to calculate the date 3 months from the input date. Can you help me to get out of this issue.Thanks in advance
import datetime
today = "2022-02-24"
three = today + datetime.timedelta(30*3)
print (three)
Also I tried using "relativedelta"
You can't add a timedelta to a string, you need to add it to a datetime instance
Note that 90 days, isn't really 3 months
from datetime import datetime, timedelta
today = "2022-02-24"
three = datetime.strptime(today, "%Y-%m-%d") + timedelta(30 * 3)
print(three) # 2022-05-25 00:00:00
three = datetime.today() + timedelta(30 * 3)
print(three) # 2022-05-24 21:32:35.048700
With relativedelta of dateutil package, you can use:
from dateutil.relativedelta import relativedelta
from datetime import date
three = date.today() + relativedelta(months=3)
Output:
>>> three
datetime.date(2022, 5, 23)
I'm trying to calculate the beginning of last five fiscal years in Python but it isn't working as expected. What am I missing here?
from datetime import datetime
from dateutil.relativedelta import relativedelta
from fiscalyear import *
a = FiscalYear(datetime.date.today().year)
print(a.start- relativedelta(years=5))
This returns
2015-10-01 00:00:00
while I'm expecting
2016-10-01 00:00:00
Also, how do I test this by passing a future date as parameter? Like, I want to pass 2021-12-22T11:23:48.167 as parameter which should return 20171001 in YYYYmmdd format.
I guess this is what you want as the second part of the question:
from datetime import datetime
from dateutil.relativedelta import relativedelta
from fiscalyear import FiscalYear
FutureDate = datetime.strptime('2030 Jun 1 21:30:00', '%Y %b %d %X')
a = FiscalYear(FutureDate.year)
print(a.start- relativedelta(years = 5))
Is there any function in python that can generate date for example 4 weeks from now or given date?
I've gone through documentation from datetime modeule but couldnt find any example that can support my question.
four_weeks = datetime.timedelta(days=4*7)
dt = datetime.datetime.now()
print(dt + four_weeks)
Here you go:
from datetime import timedelta
from datetime import datetime
today = datetime.today()
print(today + timedelta(weeks=1))
I think the thing you're looking for is timedelta.
from datetime import timedelta
def add_weeks(dt, n_weeks):
n_days = 7 * n_weeks
return dt + timedelta(days=n_days)
In python datetime module has a class called datetime which represents a date + time, an point on time line. There is another class called timedelta that represents difference between two dates (datetiems).
You can add a date with a timedelta.
example code:
from datetime import datetime, timedelta
now = datetime.now()
duration = timedelta(days=28)
target = now + duration
print(target)
I'd like to get the break of a variable by year, month and day. Here's what I got:
import datetime
from datetime import date, timedelta
yesterday = date.today() - timedelta(1)
print (yesterday)
year = datetime.date.yesterday.year
month = datetime.date.yesterday.month
day=datetime.date.yesterday.day
print (year)
print (month)
print (day)
I'm getting an error that datetime.date has no attribute. I'm a total noob at python and I'm stuck, any help is appreciated
you were close
import datetime
from datetime import date, timedelta
yesterday = date.today() - timedelta(1)
print (yesterday)
year = yesterday.year
month = yesterday.month
day=yesterday.day
print (year)
print (month)
print (day)
result is
2019-03-10
2019
3
10
You can use strftime method
A simple example:
>>> from datetime import datetime
>>> now = datetime.utcnow()
>>> year_month_day_format = '%Y-%m-%d'
>>> now.strftime(year_month_day_format)
'2020-11-06'
>>> hour_minute_format = '%H:%M'
>>> now.strftime(hour_minute_format)
'22:54'
Hopping, it will help someones
You can also simplify your import statements like so:
from datetime import datetime, timedelta
yesterday = datetime.today() - timedelta(1)
print(yesterday)
year = yesterday.year
month = yesterday.month
day = yesterday.day
print(year)
print(month)
print(day)
You will get the output:
2019-03-10 21:19:36.695577
2019
3
10
For current day
import datetime
current_datetime=datetime.datetime.now()
print("current_year:{}".format(current_datetime.year))
print("current_month:{}".format(current_datetime.month))
print("current_day:{}".format(current_datetime.day))
If you want in this format for example "10-Oct-2018". You can try this code for current day.
from datetime import datetime, timezone
now_utc = datetime.now(timezone.utc)
year = now_utc.strftime("%Y")
month = now_utc.strftime("%b")
day = now_utc.strftime("%d")
result = day+"-"+month+"-"+year
print(result)
I have following string of date "2018-05-08" and I would like to convert it into the datetime format of python like: "2018-05-08T00:00:00.0000000". I understand I can combine the string like:
> date = "2018-05-08"
> time = "T00:00:00.0000000"
> date+time
'2018-05-08T00:00:00.0000000'
But is there pythonic way of doing it where I can use libraries like datetime?
You can use datetime module like this example:
import datetime
date = "2018-05-08"
final = datetime.datetime.strptime(date, '%Y-%m-%d').strftime('%Y-%m-%dT%H:%M:%S.%f')
print(final)
Output:
2018-05-08T00:00:00.000000
For more details visit datetime's documentation
Use this code
from datetime import date
from datetime import datetime
d = date.today()
datetime.combine(d, datetime.min.time())
from datetime import datetime
datetime.strftime(datetime.strptime('2018-05-08','%Y-%m-%d'),'%Y-%m-%dT%H:%M:%S.%f')
OUTPUT:
'2018-05-08T00:00:00.000000'
from datetime import datetime
date_as_datetime = datetime.strptime("2018-05-08", '%Y-%m-%d')
date_as_string = date_as_datetime.strftime('%Y-%m-%dT%H:%M:%S')
print(date_as_string)
Out:
'2018-05-08T00:00:00'
See strptime/strftime options here: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
Yes, you can use the datetime module and the strftime method from that module. Here is an example below if we want to format the current time.
import datetime
current_time = datetime.datetime.now()
formatted_time = datetime.datetime.strftime(current_time, "%Y-%h-%d %H:%m:%S")
print(formatted_time)
Here is the output:
2018-May-08 13:05:16