Constantin
Constantin

Reputation: 915

strftime hour format %H returns 00 no matter the hour

Following this simple function

def get_yyyy_mm_dd_hh():
    return date.today().strftime('%Y-%m-%d#%H')

print(
    get_yyyy_mm_dd_hh()
)

I keep getting hour to 00 no matter what. Example output:

2021-08-12#00

I have tested on different unix machines, online python compilers as well as my local PC, whereas the current hour is 13 or 1PM

What am I doing wrong?

Upvotes: 0

Views: 495

Answers (1)

Yevhen Kuzmovych
Yevhen Kuzmovych

Reputation: 12130

Because today() returns date object which defaults the time to 00:00:00. You probably need now() that'll return current time (datetime object):

from datetime import datetime

def get_yyyy_mm_dd_hh():
    return datetime.now().strftime('%Y-%m-%d#%H')

print(
    get_yyyy_mm_dd_hh()
)

Upvotes: 2

Related Questions