这篇文章主要介绍Python根据日期判断是周几的方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

python作为现在很流行的一门语言,学好python是很有必要的,下面是根据日期判断周几的个人总结的一些方法,可供参考。

Python如何根据日期判断周几

Python判断周几主要使用了time, datetime库,具体实现方法如下。

方法一:

#!/usr/bin/python#-*-coding:utf-8-*-fromdatetimeimportdatetimetoday=datetime.now().weekday()+1print(today)week=datetime.strptime("20191018","%Y%m%d").weekday()+1print(week)

方法二:

#!/usr/bin/python#-*-coding:utf-8-*-fromdatetimeimportdatetimeapply_time=20191018apply_time=datetime.utcfromtimestamp(apply_time)time_str=apply_time.strftime("%Y-%m-%d%H:%M:%S")apply_hour=time_str.split("")[1].split(":")[0]apply_week=datetime.strptime(time_str.split("")[0],"%Y-%m-%d").weekday()print(apply_week)

方法三:

#!/usr/bin/python#-*-coding:utf-8-*-importtime,datetimedefget_week_day(date):week_day_dict={0:'星期一',1:'星期二',2:'星期三',3:'星期四',4:'星期五',5:'星期六',6:'星期天',}day=date.weekday()returnweek_day_dict[day]print(get_week_day(datetime.datetime.now()))

以上是Python根据日期判断是周几的方法的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!