
[英]How to find the last date of the month from current month (excel version of EOMONTH) in PYTHON?
[英]How to get the EOMONTH(End of month) of the current date using Python3
我有一个 SQL 服务器存储过程,我正在尝试使用 python 复制它。 我试图复制的一件事是以下功能:
DECLARE @Anchor_DT as DATE =EOMONTH(Getdate(),-1);
这是我在 Python3 中尝试过的:
import datetime
datetime.date (2000, 3, 1) - datetime.timedelta (days = 1)
输出:
datetime.date(2000, 2, 29)
问题是,我必须输入日期(200,3,1)。 我希望能够获取当前日期并输出月末。 这是我尝试过但无济于事的方法:
import datetime
datetime.date.now() - datetime.timedelta (days = 1)
任何建议或推荐的解决方案?
关于什么:
import datetime
now = datetime.datetime.today()
print(datetime.datetime((now.year + (now.month // 12)), (now.month + 1) % 12, 1) - datetime.timedelta(days = 1))
或者,您可以使用calendar.monthrange方法:
import calendar
import datetime
now = datetime.datetime.today()
_, lastday = calendar.monthrange(now.year, now.month)
print(datetime.datetime(now.year, now.month, lastday))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.