Answer updated to Python 3.7 and more
Here is how you can turn a date-and-time object
[aka datetime.datetime
object, the one that is stored inside models.DateTimeField
django model field]
into a date object [aka datetime.date
object]:
from datetime import datetime
#your date-and-time object
# let's supposed it is defined as
datetime_element = datetime[2020, 7, 10, 12, 56, 54, 324893]
# where
# datetime_element = datetime[year, month, day, hour, minute, second, milliseconds]
# WHAT YOU WANT: your date-only object
date_element = datetime_element.date[]
And just to be clear, if you print those elements, here is the output :
print[datetime_element]
2020-07-10 12:56:54.324893
print[date_element]
2020-07-10
In this article, you will learn to get today's date and current date and time in Python. We will also format the date and time in different formats using strftime[] method.
Video: Dates and Times in Python
There are a number of ways you can take to get the current date. We will use the
date
class of the datetime module to accomplish this task.
Example 1: Python get today's date
from datetime import date
today = date.today[]
print["Today's date:", today]
Here, we imported the date
class from the datetime
module. Then, we used the date.today[]
method to get the current local date.
By the way, date.today[]
returns a date
object, which is assigned to the today variable in the above
program. Now, you can use the strftime[] method to create a string representing date in different formats.
Example 2: Current date in different formats
from datetime import date
today = date.today[]
# dd/mm/YY
d1 = today.strftime["%d/%m/%Y"]
print["d1 =", d1]
# Textual month, day and year
d2 = today.strftime["%B %d, %Y"]
print["d2 =", d2]
# mm/dd/y
d3 = today.strftime["%m/%d/%y"]
print["d3 =", d3]
# Month abbreviation, day and year
d4 = today.strftime["%b-%d-%Y"]
print["d4 =", d4]
When you run the program, the output will be something like:
d1 = 16/09/2019 d2 = September 16, 2019 d3 = 09/16/19 d4 = Sep-16-2019
If you need to get the current date and time, you can use datetime
class of the datetime
module.
Example 3: Get the current date and time
from datetime import datetime
# datetime object containing current date and time
now = datetime.now[]
print["now =", now]
# dd/mm/YY H:M:S
dt_string = now.strftime["%d/%m/%Y %H:%M:%S"]
print["date and time =", dt_string]
You will gate output like below.
now = 2021-06-25 07:58:56.550604 date and time = 25/06/2021 07:58:56
Here, we have used datetime.now[]
to get the current date and time. Then, we used strftime[]
to create a string representing date and time in another format.
In this article, we are going to see how to convert DateTime to date in Python. For this, we will use the strptime[] method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date[] function and dt.date from Pandas in Python.
Method 1: Convert DateTime to date in Python using DateTime
Classes for working with date and time are provided by the Python Datetime module. Numerous capabilities to deal with dates, times, and time intervals are provided by these classes. Python treats date and DateTime as objects, so when you work with them, you’re really working with objects rather than strings or timestamps.
Syntax of strptime[]
Syntax: datetime.strptime[]
Parameters:
- arg: It can be integer, float, tuple, Series, Dataframe to convert into datetime as its datatype
- format: This will be str, but the default is None. The strftime to parse time, eg “%d/%m/%Y”, note that “%f” will parse all the way up to nanoseconds.
Example 1: Convert DateTime to date
In this example, We have created a datetime_str which is “24AUG2001101010”, and its format is “%d%b%Y%H%M%S”.
Python3
import
datetime
from
datetime
import
datetime
datetime_str
=
"24AUG2001101010"
print
[
"datetime string : {}"
.
format
[datetime_str]]
datetime_obj
=
datetime.strptime[datetime_str,
"%d%b%Y%H%M%S"
]
print
[datetime_obj]
date
=
datetime_obj.date[]
print
[date]
Output:
datetime string : 24AUG2001101010 2001-08-24 10:10:10 2001-08-24
Example 2: Convert DateTime with a numeric date.
In this example, We have created a datetime_str which is “100201095407”, and its format is “%d%m%y%H%M%S”.
Python3
import
datetime
from
datetime
import
datetime
datetime_str
=
"100201095407"
print
[
"datetime string : {}"
.
format
[datetime_str]]
datetime_obj
=
datetime.strptime[datetime_str,
"%d%m%y%H%M%S"
]
print
[datetime_obj]
date
=
datetime_obj.date[]
print
[date]
Output:
datetime string : 100201095407 2001-02-10 09:54:07 2001-02-10
Example 3: Convert DateTime with the current date.
In this example, we take the present date and time and extracted its date from the object.
Python3
from
datetime
import
datetime
datetime_obj
=
datetime.now[]
print
[datetime_obj]
date
=
datetime_obj.date[]
print
[date]
Output:
2021-08-07 06:30:20.227879 2021-08-07
Method 2: Convert DateTime to date in Python using Pandas
Pandas provide a different set of tools using which we can perform all the necessary tasks on date-time data. Let’s try to understand with the examples discussed below.
Example:
The date value and the DateTime value are both displayed in the output using the print command. The DateTime values are first added to a column of a Pandas DataFrame. The DateTime value is then converted to a date value using the dt.date[] function.
Python3
import
pandas as pd
df
=
pd.DataFrame[{
'time'
: [
'2022-7-16 11:05:00'
,
'2025-7-18 12:00:30'
]}]
print
[df]
df[
'time'
]
=
pd.to_datetime[df[
'time'
]].dt.date
print
[df]
Output:
time 0 2022-7-16 11:05:00 1 2025-7-18 12:00:30 time 0 2022-07-16 1 2025-07-18