close

How do you extract only the date from a python datetime?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How do you extract only the date from a python datetime in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How do you extract only the date from a python datetime?

  1. How do you extract only the date from a python datetime?

    You can use date and time methods of the datetime class to do so:
    >>> from datetime import datetime >>> d = datetime.now() >>> only_date, only_time = d.date(), d.time()

  2. extract only the date from a python datetime

    You can use date and time methods of the datetime class to do so:
    >>> from datetime import datetime >>> d = datetime.now() >>> only_date, only_time = d.date(), d.time()

Method 1

You can use date and time methods of the datetime class to do so:

>>> from datetime import datetime
>>> d = datetime.now()
>>> only_date, only_time = d.date(), d.time()
>>> only_date
datetime.date(2015, 11, 20)
>>> only_time
datetime.time(20, 39, 13, 105773)

Here is the datetime documentation.

Applied to your example, it can give something like this:

>>> milestone["only_date"] = [d.date() for d in milestone["datetime"]]
>>> milestone["only_time"] = [d.time() for d in milestone["datetime"]]

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read