Getting Current Year, Month, Day, Time using Python

Python’s built-in module datetime can be used for knowing what is current Year, Month, Day, Time.

Python Code for Getting Current Year

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

Python Code for Getting Current Month

import datetime
current_month = datetime.datetime.now().month
print(current_month)

Python Code for Getting Current Day

import datetime
current_day = datetime.datetime.now().day
print(current_day)

Python Code for Getting Current Time

import datetime
current_hour = datetime.datetime.now().hour
current_minute = datetime.datetime.now().minute
current_second = datetime.datetime.now().second
print(str(current_hour) + ":" + str(current_minute) + ":" + str(current_second))

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts