type object 'datetime.datetime' has no attribute 'datetime'

前端 未结 8 1577
情深已故
情深已故 2020-11-28 03:37

I have gotten the following error:

type object \'datetime.datetime\' has no attribute \'datetime\'

On the following line:

<
相关标签:
8条回答
  • 2020-11-28 04:15

    You should really import the module into its own alias.

    import datetime as dt
    my_datetime = dt.datetime(year, month, day)
    

    The above has the following benefits over the other solutions:

    • Calling the variable my_datetime instead of date reduces confusion since there is already a date in the datetime module (datetime.date).
    • The module and the class (both called datetime) do not shadow each other.
    0 讨论(0)
  • 2020-11-28 04:22

    You should use

    date = datetime(int(year), int(month), 1)
    

    Or change

    from datetime import datetime
    

    to

    import datetime
    
    0 讨论(0)
  • 2020-11-28 04:22

    If you have used:

    from datetime import datetime
    

    Then simply write the code as:

    date = datetime(int(year), int(month), 1)
    

    But if you have used:

    import datetime
    

    then only you can write:

    date = datetime.datetime(int(2005), int(5), 1)
    
    0 讨论(0)
  • 2020-11-28 04:22
    from datetime import datetime
    import time
    from calendar import timegm
    d = datetime.utcnow()
    d = d.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
    utc_time = time.strptime(d,"%Y-%m-%dT%H:%M:%S.%fZ")
    epoch_time = timegm(utc_time)
    
    0 讨论(0)
  • 2020-11-28 04:24

    For python 3.3

    from datetime import datetime, timedelta
    futuredate = datetime.now() + timedelta(days=10)
    
    0 讨论(0)
  • 2020-11-28 04:34

    I found this to be a lot easier

    from dateutil import relativedelta
    relativedelta.relativedelta(end_time,start_time).seconds
    
    0 讨论(0)
提交回复
热议问题