Get current date (now)

The following example queries and outputs the individual elements of a datetime object, such as year, month, day, hours, minutes and seconds of the current time.

Example 1

import datetime

dt = datetime.datetime.now()

print("current datetime: " + str(dt))
print("current year: " + str(dt.year))
print("current month: " + str(datetime.datetime.now().month))
print("current day: " + str(dt.day))
print("hours: " + str(dt.hour))
print("minutes: " + str(dt.minute))
print("seconds: " + str(dt.second))
Output
current datetime: 2022-12-10 11:29:43.074482
current year: 2022
current month: 12
current day: 10
hours: 11
minutes: 29
seconds: 43

Example 2

import datetime

dt = datetime.datetime.now()

day = dt.day
month = dt.month
year = dt.year
hour = dt.hour
minute = dt.minute
second = dt.second

print("Date: " + str(day) + "/" + str(month) + "/" + str(year))
print("Time: " + str(hour) + ":" + str(minute) + ":" + str(second))
Output
Date: 11/12/2022
Time: 9:31:2