Issue
I have a SQL server stored procedure and I am trying to replicate it using python. One of the things I am trying to replicate is the following function:
DECLARE @Anchor_DT as DATE =EOMONTH(Getdate(),-1);
Here is what I have tried in Python3:
import datetime
datetime.date (2000, 3, 1) - datetime.timedelta (days = 1)
Output:
datetime.date(2000, 2, 29)
The thing is, I have to enter the date (200,3,1). I want to be able to pick up the current date and output the End of month. Here is what I tried but to no avail:
import datetime
datetime.date.now() - datetime.timedelta (days = 1)
Any suggestions or recommended solutions?
Solution
What about:
import datetime
now = datetime.datetime.today()
print(datetime.datetime((now.year + (now.month // 12)), (now.month + 1) % 12, 1) - datetime.timedelta(days = 1))
Or, you could use the calendar.monthrange method:
import calendar
import datetime
now = datetime.datetime.today()
_, lastday = calendar.monthrange(now.year, now.month)
print(datetime.datetime(now.year, now.month, lastday))
Answered By - olinox14
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.