简体   繁体   English

python 中的 UTC 时间和日期时间

[英]UTC time in python with datetime

How can I use datetime.utcnow() and datetime.date.today() together?如何一起使用datetime.utcnow()datetime.date.today() In case I am running code A it throws error and Code B other one.如果我正在运行代码 A,它会抛出错误,而代码 B 会抛出另一个错误。 I want to use both of these in my code.我想在我的代码中使用这两个。

A一个

from datetime import datetime, timedelta

path = datetime.utcnow().strftime(f'{category}/%Y%m%d/%H:%M')
for year in range(2014, 2018):
    for month in range(start_month_number, 13):
        this_month = datetime.date.today().replace(year=year, month=month, day=1)
        print(this_month)

error - AttributeError: 'method_descriptor' object has no attribute 'today'

B

import datetime
path = datetime.utcnow().strftime(f'{category}/%Y%m%d/%H:%M')
for year in range(2014, 2018):
    for month in range(start_month_number, 13):
        this_month = datetime.date.today().replace(year=year, month=month, day=1)
        print(this_month)

 error- AttributeError: module 'datetime' has no attribute 'utcnow'

Code B run fine in case there is no line -->curryear = datetime.utcnow().strftime('%Y')如果没有行,代码 B 运行良好 -->curryear = datetime.utcnow().strftime('%Y')

Either import the module that you need, or the classes you need from the module - not both.要么导入你需要的模块,要么从模块中导入你需要的类——不能同时导入。 Then, write your code according to what you have imported:然后,根据您导入的内容编写代码:

A: A:

from datetime import datetime, date

path = datetime.utcnow().strftime(f'{category}/%Y%m%d/%H:%M')
for year in range(2014, 2018):
    for month in range(1, 13):
        this_month = date(year=year, month=month, day=1)
        print(this_month)

or B:或 B:

import datetime

path = datetime.datetime.utcnow().strftime(f'{category}/%Y%m%d/%H:%M')
for year in range(2014, 2018):
    for month in range(1, 13):
        this_month = datetime.date(year=year, month=month, day=1)
        print(this_month) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 Python 日期时间 object 转换为本地时间 - Converting a Python datetime object that is in utc to local time 将 UTC 时间添加/减去日期时间“时间”列 - Add/Subtract UTC Time to Datetime 'Time' column 通过 Python,当系统时钟是 UTC 时,根据 DST 调整本地时间? 必须使用时间模块而不是日期时间 - Via Python, get local time adjusted for DST, when system clock is UTC? Must use time module not datetime AWS Glue 作业,Python 脚本 datetime.today().strftime(%y/%m/%d) 获取 UTC 日期和时间? - AWS Glue job, Python script for datetime.today().strftime(%y/%m/%d) getting UTC date & time? 在 Python 3 中将 EST 日期时间转换为 UTC 时间戳 - Convert EST Datetime to UTC Timestamp in Python 3 如何在 Python 中将 UTC/扩展格式转换为日期时间 - How to convert UTC/extended format to datetime in Python 如何将字符串列表中的 ISO 8601 日期时间转换为 python 中的 utc 日期时间? - How to convert ISO 8601 datetime in string list to utc datetime in python? 如何在python中将当地时间转换为UTC时间 - How to convert the local time into utc time in python 如何使用 python 将 UTC 时间转换为相应的 UTC 日期和时间? - How to convert times in UTC to a corresponding Date and time in UTC using python? 将 UTC 日期时间转换为 UTC 纪元 - Convert UTC datetime to UTC epoch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM