hour in python code example

Example 1: get the current year in python

import datetime
now = datetime.datetime.now().year
print(now)

Example 2: get hour python

import datetime
now = datetime.datetime.now()
print now.year, now.month, now.day, now.hour, now.minute, now.second
# 2015 5 6 8 53 40

Example 3: python get day month year

import datetime
now = datetime.datetime.now()
print(now.year, now.month, now.day, now.hour, now.minute, now.second)

Example 4: python date and time

from datetime import datetime
now = datetime.now()
print (now.strftime("%Y-%m-%d %H:%M:%S"))


Output: 2020-06-19 10:34:45

Example 5: date.month date time

import datetime
dt = datetime.datetime.today()
print dt.year
# Output: 2016

print dt.month
# Output: 8

print dt.day
# Output: 22