• 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 / Remove Time from Datetime in Python

Remove Time from Datetime in Python

March 5, 2022 Leave a Comment

To remove the time from a datetime object in Python, convert the datetime to a date using date().

import datetime

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

print(currentDateTime)
print(currentDateWithoutTime)

#Output:
2022-03-05 15:33:11.283729
2022-03-05

You can also use strftime() to create a string from a datetime object without the time.

import datetime

currentDateTime = datetime.datetime.now()
currentDateWithoutTime = currentDateTime.strftime('%Y-%m-%d')

print(currentDateTime)
print(currentDateWithoutTime)

#Output:
2022-03-05 15:33:11.283729
2022-03-05

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

With Python, we can easily remove the time from a datetime variable.

To get rid of the time, we just need to convert the datetime to a date. To do so, use the datetime date() function.

Below is a simple example of how to get remove the time from a datetime in Python.

import datetime

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

print(currentDateTime)
print(currentDateWithoutTime)

#Output:
2022-03-05 15:33:11.283729
2022-03-05

Using strfttime to Remove the Time from Datetime in Python

The Python strftime() function is very useful when working with date and datetime variables. strftime() accepts a string representing the format of a date and returns the date as a string in the given format.

We can use strftime() to easily remove the time from datetime variables.

For example, if you want to print out the date in the format “YYYY-MM-DD”, we pass “%Y-%m-%d” to strfttime() and no time is printed.

Below is a simple Python example of how to print the date without time using strftime().

import datetime

currentDateTime = datetime.datetime.now()
currentDateWithoutTime = currentDateTime.strftime('%Y-%m-%d')

print(currentDateTime)
print(currentDateWithoutTime)

#Output:
2022-03-05 15:33:11.283729
2022-03-05

Hopefully this article has been useful for you to learn how to use Python to remove the time from datetime variables.

Other Articles You'll Also Like:

  • 1.  Using Python to Check If a Number is a Perfect Square
  • 2.  Length of Tuple Python – Find Tuple Length with Python len() Function
  • 3.  Using Python to Get Queue Size
  • 4.  How to Divide Two Numbers in Python
  • 5.  Python issubset() Function – Check if Set is Subset of Another Set
  • 6.  Fibonacci Sequence in Python with for Loop
  • 7.  Python asinh – Find Hyperbolic Arcsine of Number Using math.asinh()
  • 8.  Cartesian Product of Two Lists in Python
  • 9.  Python power function – Exponentiate Numbers with math.pow()
  • 10.  Generate Random Float Between 0 and 1 Using 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

x