• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Get Current Datetime in Python

Get Current Datetime in Python

August 19, 2022 Leave a Comment

To get the current datetime in Python, you can use the datetime module and datetime.now() function which returns the date and time.

import datetime

currentDateTime = datetime.datetime.now()

print(currentDateTime)

#Output:
2022-08-19 08:33:11.283729

If you want to get the current date, then you can convert the datetime to a date with date().

import datetime

currentDateTime = datetime.datetime.now()
currentDate = currentDateTime.date()

print(currentDate)

#Output:
2022-08-19

When working in Python, many times we need to create variables which represent dates and times. When creating and displaying values related to date and times, sometimes we need to display the current date and time.

To get the current datetime in Python, you can use the datetime module.

The datetime module has many great functions which allow you to work with dates and times in Python.

To get the current datetime in Python, you can use the datetime module and datetime.now() function which returns the date and time.

Below shows you how to get the current datetime in Python.

import datetime

currentDateTime = datetime.datetime.now()

print(currentDateTime)

#Output:
2022-08-19 08:33:11.283729

How to Get Current Date in Python

If you only want to get the current date and don’t care about the time, you can use the datetime date() function.

The datetime date() function removes the time from a datetime variable and returns just the date.

Below shows you how to get the current date in Python.

import datetime

currentDateTime = datetime.datetime.now()
currentDate = currentDateTime.date()

print(currentDate)

#Output:
2022-08-19

Hopefully this article has been useful for you to learn how to get the current datetime in Python.

Other Articles You'll Also Like:

  • 1.  Python Print List – Printing Elements of List to the Console
  • 2.  Draw Star in Python Using turtle Module
  • 3.  Get Random Letter in Python
  • 4.  Check if Variable is None in Python
  • 5.  How to Combine Dictionaries in Python
  • 6.  pandas min – Find Minimum Value of Series or DataFrame
  • 7.  Using Python to Get Home Directory
  • 8.  How to Remove Vowels from a String in Python
  • 9.  Using Python to Split String by Tab
  • 10.  Check if String Contains Numbers in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy