带有日历周数的 Python datetime strptime

标签 python datetime

根据资源,将包含“year-week_number”(如2015-20)的字符串转换为datetime对象的方法如下:

from datetime import datetime
datetime.strptime("2015-20", "%Y-%W")

但是,这并没有给我正确的结果。我得到的是 日期时间.日期时间(2015, 1, 1, 0, 0)

另外:

datetime.strftime(datetime.strptime("2015-20", "%Y-%W"). "%Y-%W")

实际返回“2015-00”。

这只是一个错误还是我忘记做某事了?

最佳答案

来自documentation :

When used with the strptime() method, %U and %W are only used in calculations when the day of the week and the year are specified.

假设您没有指定星期几,根据文档,%W 参数将被忽略。

将工作日添加到任何你想要的,我把星期一:

>>> datetime.strptime("2015-20-1", "%Y-%W-%w")
datetime.datetime(2015, 5, 18, 0, 0)

关于带有日历周数的 Python datetime strptime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41753297/

相关文章:

python - 确保只有一个程序实例正在运行

python - 调整大小时使用 PIL 保留图像的 exif 数据(创建缩略图)

PHP 从前 24 小时 sql 数据库中选择数据

sql - 我想要小时,分钟,两个日期时间的第二个差异

c# - DateTime.UtcNow 的结果不一致

python - libvirt-python CentOS 的构建轮失败

python - 将 python 代码模块化为可重用的函数

Python pickle - 它是如何损坏的?

java - 无法解析 DateTime - 由于时区偏移转换(欧洲/柏林)导致的非法时刻

php日期转换函数的月份和日期值问题