简体   繁体   English

如何在python中确定当前和上周的开始日期?

[英]How to determine the date of Starting day of current and last week in python ?

I need to find the date of the starter day of current and last week. 我需要找到当前和上周的入门日的日期。

Here, business day of a week starts from SUNDAY. 在这里,工作日从SUNDAY开始。 So, for today the date I want is to be "07-16-2017" as "mm--dd--yyyy" format. 因此,今天我想要的日期是“07-16-2017”为“mm - dd - yyyy”格式。 I can get today's date easily from datetime.datetime.now().strftime ("%Y-%m-%d") but from the sysdate I have to pull out the starter day of the week. 我可以从datetime.datetime.now().strftime ("%Y-%m-%d")轻松获得今天的日期,但是从sysdate我必须提取一周的开始日期。

I need the starter day's date for last week as well. 我也需要上周的首发日期。

There is no default method for week information in datetime. datetime中没有用于周信息的默认方法。 Is there any other method in any package in python to determine the required information ? python中的任何包中是否有任何其他方法来确定所需的信息?

You can use calendar.firstweekday() to check what the first day of the week is on that computer (0 is Monday, 6 is Sunday). 您可以使用calendar.firstweekday()来检查该计算机上一周的第一天是什么(0表示星期一,6表示星期日)。

1) Let's say that firstweekday returned 1 (Sunday). 1)假设firstweekday返回1日(星期日)。 Then you can use 然后你可以使用

date.today() - datetime.timedelta(days=date.today().isoweekday() % 7)

to compute the date of the last Sunday. 计算最后一个星期天的日期。

2) Let's say that firstweekday returned 0 (Monday). 2)假设firstweekday返回0 (星期一)。

date.today() - datetime.timedelta(days=date.today().isoweekday() % 7 - 1)

to compute the date of the last Monday. 计算上周一的日期。

Hope this gives you some direction. 希望这会给你一些指导。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM