python - 如何从 python 中的日期时间对象获取 3 个字母格式的星期几?

标签 python

我正在编写一个脚本,我必须在 Python 中使用日期时间对象。在某些时候,我有这些对象之一,我需要以 3 个字母的格式(即星期二、星期三等)获取星期几(这是一个数字值)。这是代码的简短示例,在 dateMatch.group() 中,我所做的只是获取通过正则表达式匹配获得的字符串片段。

from datetime import datetime

day = dateMatch.group(2)
month = dateMatch.group(3)
year = dateMatch.group(4)
hour = dateMatch.group(5)
minute = dateMatch.group(6)
second = dateMatch.group(7)

tweetDate = datetime(int(year), months[month], int(day), int(hour), int(minute), int(second))

从那个日期时间对象我得到一个数字日值(即 18),我需要将它转换为(即星期二)。

谢谢!

最佳答案

http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

date, datetime, and time objects all support a strftime(format) method, to create a string representing the time under the control of an explicit format string.

...

%a — Locale’s abbreviated weekday name.

>>> datetime.datetime.now().strftime('%a')
   'Wed'

关于python - 如何从 python 中的日期时间对象获取 3 个字母格式的星期几?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15509617/

相关文章:

python - 确定信号中是否包含特定频率

python - 模拟 AWS lambda 和 boto3

python - 在 python 中移动列

python - 从另一个文本文件中搜索一个文件中列出的字符串?

python - 将整数数组转换为 unicode

python - PyCryptodome 错误 : MAC Check Failed

python - PyQT - 定位和显示自定义小部件

python - 值错误 : Unable to configure filter 'require_debug_false' : Cannot resolve 'django.utils.log.RequireDebugFalse' : No module named RequireDebugFalse

python - 如何使用 raw_input 获取汉字

python - 当准确度已经是 1.0 时停止 Keras 训练