如何获得给定日期的星期几? [英] How do I get the day of week given a date?

查看:137
本文介绍了如何获得给定日期的星期几?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找出以下内容:给定日期的
datetime 对象),星期几是什么?

I want to find out the following: given a date (datetime object), what is the corresponding day of the week?

例如,星期日是第一天,星期一:第二天..依此类推

For instance, Sunday is the first day, Monday: second day.. and so on

然后输入

>>> today = datetime.datetime(2017, 10, 20)
>>> today.get_weekday()  # what I look for

输出可能是 6 (因为它是星期五)

The output is maybe 6 (since it's Friday)

推荐答案

使用 weekday()

>>> import datetime
>>> datetime.datetime.today()
datetime.datetime(2012, 3, 23, 23, 24, 55, 173504)
>>> datetime.datetime.today().weekday()
4

来自文档


将星期几作为整数返回,其中星期一为0,星期日为6。

Return the day of the week as an integer, where Monday is 0 and Sunday is 6.

这篇关于如何获得给定日期的星期几?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆