close

How To convert a datetime to date in Python

In this tutorial we will learn about how To convert a datetime to date in Python. By using some methods you can get the current date and current time. here are some methods which will help you. So without wasting time lets learn about of this.

How To convert a datetime to date in Python

These methods will help you to get the day, month and year in current date and it will also gives you the current time in hour, minute and second format. Lets see this.

  1. convert a datetime to date in Python

    To convert a datetime to date in Python just Use strptime(). By using strptime() you can convert a datetime to date in python. lets learn this by given below example:
    import datetime from datetime import datetime mystr = "24JAN2022104518" print("datetime string : {}".format(mystr)) datetime_obj = datetime.strptime(mystr, "%d%b%Y%H%M%S") print(datetime_obj) date = datetime_obj.date() print(date) Output : datetime string : 24JAN2022104518 2022-01-24 10:45:18 2022-01-24

  2. How To convert a datetime to date in Python

    To convert a datetime to date in Python just use this method. This method is for to get the current date time. It will give you the current date and time. lets learn how we can get this. from datetime import datetime current_datetime = datetime.now() print(current_datetime) date = current_datetime.date() print(date) Output : 2022-02-19 13:55:57.085715 2022-02-19

Method 1: Use strptime()

By using strptime() you can convert a datetime to date in python. This method use for to convert the string into a datetimes. First of all you have to create a string then by using strptime() you can get the day month and year in your output and you can also get the current time. Its very easy to use. lets learn this by given below example:

import datetime
from datetime import datetime
mystr = "24JAN2022104518"
print("datetime string : {}".format(mystr))
datetime_obj = datetime.strptime(mystr, 
                                 "%d%b%Y%H%M%S")
print(datetime_obj)
date = datetime_obj.date()
print(date)

Output :

datetime string : 24JAN2022104518
2022-01-24 10:45:18
2022-01-24

Method : 2

This method is for to get the current date time. It will give you the current date and time. In this method from datetime you can get the date month and year and also can get the current time. lets learn how we can get this.

from datetime import datetime
current_datetime = datetime.now()
print(current_datetime)
date = current_datetime.date()
print(date) 

Output :

2022-02-19 13:55:57.085715
2022-02-19

Method 3 : Use datetime.date()

datetime.date() function is used for to get the current time and current date. It has three parameters. Day, month and year. And it also can get the current time in hour, minute and seconds also. You can see this by given below example.

import datetime
print(datetime.datetime.now())
print(datetime.datetime.now().date())

Output :

2022-02-21 14:17:45.869347
2022-02-21

Method 4: Use date.today()

date.today() is used to get the current date. By using this you can only get the date month and year. It can not be used to get the current time. Lets learn about of this.

from datetime import date
from datetime import datetime
mydate = date.today()
mydatetime = datetime(mydate.year, mydate.month, mydate.day)
print(mydatetime)

Output :

2022-02-21 00:00:00

Conlusion

Hope all 6 Method Are Useful For You. Comment Below Which Method You Used To Convert String To Datetime In Python. Thank You.

Also Read

Leave a Comment