Python:如何转换 time.strftime 以显示 UTC 而不是本地时区?

标签 python datetime date-conversion

我正在尝试打开一个名称根据 UTC 日期时间更改的文件。

我使用以下命令获取文件名:

datetoday = time.strftime("%Y%m%d%H%M")
print datetoday
filename = "C:\Users\Downloads\filename-%s.csv" % datetoday
inputfile = open(filename)

问题是这是在本地 (EST) 时区而不是我想要的 UTC 中打印文件名。

有没有办法将 time.strftime() 转换为显示 UTC 时区而不是本地时区?

谢谢。

最佳答案

代替:

import time
datetoday = time.strftime("%Y%m%d%H%M")

尝试:

from datetime import datetime
datetoday = datetime.utcnow().strftime("%Y%m%d%H%M")

关于Python:如何转换 time.strftime 以显示 UTC 而不是本地时区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41684483/

相关文章:

python - 将元组扩展为参数

python - 如何暂停pytest计时器?

c# - 为什么部署在 Azure 上的 Web 应用程序的日期格式发生变化?(ASP .NET Core)

oracle - ORA-00932 : inconsistent datatypes: expected NUMBER got DATE in oracle stored procedure

c - 如何转换日期格式: 2019-08-22 16:16:08] to the format: aammjjhhmmss in C language

python:单词和词形词典

python - "Expected string or buffer"使用 Beautiful Soup 时出错

python - 根据另一列拆分 DataFrame 中的行(每小时)

PHP时区转换问题

python - 按 pandas 分组以获得从源到最终目的地的路径