How do i print only date from datetime in python?

I have this code :

>>> import datetime
>>> l = '2011-12-02'
>>> t = datetime.datetime.strptime[l, '%Y-%m-%d']
>>> print t
2011-12-02 00:00:00

my question is, it is possible that only the 2011-12-02 will be printed?

asked Dec 3, 2011 at 5:43

1

>>> t.strftime['%Y-%m-%d']
'2011-12-02'

answered Dec 3, 2011 at 5:46

Dan D.Dan D.

71.4k13 gold badges98 silver badges118 bronze badges

1

With strftime[]:

print t.strftime['%Y-%m-%d']

Chris Morgan

81.3k22 gold badges200 silver badges208 bronze badges

answered Dec 3, 2011 at 5:45

BlenderBlender

280k51 gold badges425 silver badges487 bronze badges

4

You have to specify the output format for the date, e.g. by using

print t.strftime["%Y-%m-%d"]

All the letter after a "%" represent a format: %d is the day number, %m is the month number, %y is the year last two digits, %Y is the all year

answered Dec 3, 2011 at 5:54

Alpha01Alpha01

8386 silver badges13 bronze badges

I think you should use like this

 d = datetime.datetime[2011,7,4]
 print '{:%Y-%m-%d}'.format[d]

or your code:

import datetime
l = '2011-12-02'
t = datetime.datetime.strptime[l, '%Y-%m-%d']
print '{:%Y-%m-%d}'.format[t]

answered Dec 3, 2011 at 5:46

hungneoxhungneox

8,90310 gold badges48 silver badges64 bronze badges

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

How do I print only the date without time in Python?

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.

How do I show only the date in Python?

Python Datetime.
❮ Previous Next ❯.
Import the datetime module and display the current date: import datetime. x = datetime.datetime.now[] ... .
Return the year and name of weekday: import datetime. x = datetime.datetime.now[] ... .
Create a date object: import datetime. ... .
Display the name of the month: import datetime. ... .
❮ Previous Next ❯.

How can I print date from datetime?

You can use from datetime import datetime , and then print datetime[]. now[]. strftime["%Y-%m-%d %H:%M"] . ... .
from datetime import date; date. today[]. ... .
my favorite is from datetime import datetime as dt and now we can play with dt.now[] – diewland..

How do I convert a datetime to a specific date in Python?

Use datetime. strftime[format] to convert a datetime object into a string as per the corresponding format . The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.

Chủ Đề