python根据时间获取周数代码实例

时间

时间和周数

import time

import datetime

# 获取今天是第几周

print(time.strftime('%W'))

# 获取当前是周几(0-6,0代表周一)

today=datetime.datetime.now().weekday()

# 获取指定日期属于当年的第几周

week=datetime.datetime.strptime('20190825','%Y%m%d').strftime('%W')

获取下周的时间范围

import datetime,calendar,pandas as pd

def get_N_day():

# 获取当前日期

today1 = datetime.date.today()

today2 = datetime.date.today

# 指定时间周期

oneday = datetime.timedelta(days=1)

oneweek = datetime.timedelta(days=7)

# 获取周一和周日的判断条件(周一为0,周日为6)

m1 = calendar.MONDAY

m2 = calendar.SUNDAY

# 获取下周周一的时间

if today1.weekday() != m1:

while today1.weekday() != m1:

today1 += oneday

else:

today1+=oneweek

# 获取下周周日的时间

if today2.weekday() != m2:

today2 += oneweek

while today2.weekday() != m2:

today2 += oneday

else:

today2 += oneweek

# 转换时间形式

nextMonday = today1.strftime('%Y%m%d')

nextSunday = today2.strftime('%Y%m%d')

# 获取下周七天的时间列表

date_list = [d.strftime("%Y-%m-%d") for d in pd.date_range(nextMonday, nextSunday, freq="D")]

# 转换呈现形式

list_date=[]

for i in date_list:

one=i.split('-')

time=['月','日']

time.insert(0,one[1])

time.insert(2,one[2])

time1=''.join(time)

list_date.append(time1)

return list_date

print(get_N_day())

easy!

以上是 python根据时间获取周数代码实例 的全部内容, 来源链接: utcz.com/z/328331.html

回到顶部