0

How can I use datetime.utcnow() and datetime.date.today() together? In case I am running code A it throws error and Code B other one. 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')

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Andrew
  • 183
  • 2
  • 14
  • A: why don't you use `datetime.today().date()` instead? Also, please clarify: what is `curryear` used for? what value has `start_month_number`? B: check your imports, here you import a module `datetime` vs. a class `datetime.datetime` in A. – FObersteiner May 12 '20 at 11:07
  • and finally: if you wish your dates to refer to UTC, use `datetime.utcnow()` consistently, i.e. replace `datetime.today()`. – FObersteiner May 12 '20 at 11:11
  • actually I can taking path in this format... path = datetime.utcnow().strftime(f'{category}/%Y%m%d/%H:%M')..if using import datetime in this case throw an error. – Andrew May 12 '20 at 11:18
  • ok, it's just a bit confusion if there are variables that are not actually part of the question/problem/error ;-) – FObersteiner May 12 '20 at 11:26
  • my bad. updated the question. – Andrew May 12 '20 at 11:32
  • coming back to your question: what are you actually trying to achieve? I mean, you could simply write `this_month = datetime.date(year=year, month=month, day=1)` - no need for `replace` or anything related to UTC. – FObersteiner May 12 '20 at 11:35
  • how can i solve this ... import datetime path = datetime.utcnow().strftime(f'{category}/%Y%m%d/%H:%M') Exception: AttributeError: module 'datetime' has no attribute 'utcnow' – Andrew May 12 '20 at 11:39

1 Answers1

0

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:

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:

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) 
FObersteiner
  • 22,500
  • 8
  • 42
  • 72