我尝试了以下操作:选择一年中的星期(current_timestamp(但这是计算从周一到周日的一周。我想要从周六到周五的一周,基于此,它应该填充周数。在蜂箱里如果我们做不到,在蟒蛇里怎么样。?
在python中我尝试过:
A = datetime.date(2020, 3, 29).isocalendar()[1]
但这再次计算了从周一到周六的一周
再尝试一次,假设1月1日总是在第1周。
from datetime import date
from calendar
def week_number(ndate, week_start=calendar.MONDAY):
new_years_day = ndate.replace(month=1, day=1)
offset = (new_years_day.weekday() - week_start) % 7 # Count back to start of week. Modulo for positive value.
all_days = (ndate - new_years_day).days + offset # Days ago week 1 started.
return (all_days // 7) + 1 # Integer division means we don't have to floor() it. Add 1 for human readable numbers.
print(week_number(date(2020, 1, 1))) # 1, always
print(week_number(date(2020, 3, 29))) # 13
print(week_number(date(2020, 3, 29), calendar.SATURDAY)) # 14
print(week_number(date(2020, 3, 27), calendar.SATURDAY)) # 13
import datetime
def week_number(ndate):
odate = datetime.datetime(ndate.year, 1, 1)
spec = odate.weekday()
alldays = (ndate - odate).days + spec - 4
mod = alldays % 7
days = (alldays - mod) / 7
if (mod > 0):
days = days + 1
return (int)days
print(week_number(datetime.datetime.today()))