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))
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)
This question already has answers here:
Python date string to date object
(9 answers)
Closed 1 year ago.
I have a string:
dat="012915"
I want to convert it to a date:
01-29-2015
I tried:
import datetime
from datetime import datetime
dat="012915"
dat = datetime.strptime(dat, '%m%d%Y').date()
dat
but failed:
ValueError: time data '01-29-15' does not match format '%m%d%Y'
%Y is for full year
%y is the short version for year
Your code is totally fine, just change the year directive to lowercase %y will do.
import datetime
from datetime import datetime
dat="012915"
dat = datetime.strptime(dat, '%m%d%y').date()
dat
I think you are looking for
import datetime
from datetime import datetime
dat="012915"
#lower %y
dat = datetime.strptime(dat, '%m%d%y').date()
print(dat)
this will give you
2015-01-29
Trying this out; changing the format string to '%m%d%y' seems to work. Looking at the python docs:
%y Year without century as a zero-padded decimal number.
%Y Year with century as a decimal number.
So the first one is what you need. Source: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
import datetime
from datetime import datetime
dat="012915"
dat = datetime.strptime(dat, '%m%d%y').date()
print(dat)
Change %Y to %y. If you want to use %Y, change dat to '01292015'.
y% is formatted as 15 while %Y is formatted as 2015.
from datetime import datetime
date_str = '012915'
date_obj = datetime.strptime(date_str, '%m/%d/%y')
print("The type of the date is now", type(date_obj))
print("The date is", date_obj)
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
It's quite simple. I like to find the previous month for the current time, but in YYYY/MM Date Format. Ideally using Datetime.
Use months with relativedelta:
>>> from dateutil.relativedelta import relativedelta
>>> (datetime.now() - relativedelta(months=1)).strftime('%Y/%m')
'2019/08'
This covers the case you said:
>> (datetime.strptime('01/03/2019', '%d/%m/%Y') - relativedelta(months=1)).strftime('%Y/%m')
'2019/02'
from datetime import datetime, timedelta
(datetime.now() - timedelta(days=30)).strftime('%Y/%m')
This may suffice depending on your needs, a more sophisticated approach may use libraries from pip, like dateutil
from dateutil.relativedelta import relativedelta
(datetime.now() - relativedelta(months=1)).strftime('%Y/%m')
Regards
Using only the datetime module from the standard library, we can get the month and year from 15 days before the start of our month:
from datetime import date, timedelta
def previous_month():
format = '%Y/%m'
d = date.today().replace(day=1) # first day of the month
return date.strftime(d-timedelta(days=15), format) # 15 days earlier is in the previous month
previous_month()
# '2019/08
this is what I am doing to get year(1880) from year column(year: 01/01/1880 12:00:00 AM),
import pandas
from datetime import date, datetime, timedelta
data = pandas.read_csv('Landings.csv')
a=data['year']
years = **datetime.datetime.a**
print years
I think the hightlited line is creating probnem anyone correct me, how to get year
I guess data["year"] returns a Series of strings, rather than a string, hence you need to apply/map a parser to to.
import pandas as pd
from datetime import datetime
data = pd.read_csv("Landings.csv")
a=data["year"]
years = [datetime.strptime(date, '%m/%d/%Y %H:%M:%S %p').year for date in a]
Alternatively. A more pandas-esque solution (basically, use the pandas.Series.applymethod instead of the list generator). This will leave you in the realm of numpy/pandas
years = data["year"].apply(lambda date: datetime.strptime(date, '%m/%d/%Y %H:%M:%S %p').year)
Example
In [1]: import pandas as pd
In [2]: from datetime import datetime
In [3]: data = pd.DataFrame({"year": ["01/01/1880 12:00:00 AM"]})
In [4]: data
Out[4]:
year
0 01/01/1880 12:00:00 AM
In [5]: a = data["year"]
In [6]: [datetime.strptime(date, '%m/%d/%Y %H:%M:%S %p').year for date in a]
Out[6]: [1880]
Since 'a' is a string, it is not a callable function in the datetime module. The following code should work.
import pandas
from datetime import date, datetime, strptime, timedelta
data = pandas.read_csv('Landings.csv')
a=data['year']
years = datetime.strptime(a, '%m/%d/%Y %H:%M:%S %p').year
print years