我正在编写一个脚本,我必须在 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/