change date formate in python

To change the date formate in python, you can use the datetime module along with the  strftime (string format time) method to format dates according to your need

import the necessary modules:

from datetime import datetime

parse a date string into a datetime object:

date_str=”2024-06-15″

date_obj=

datetime.strptime(date_str,”%y-%m-%d”)

formate the datetime object into a new string format:

new_date_str=

date_obj.strftime(“%d,%m,%y”)

print(new_date_str)

output:

15/06/2024

from datetime import datetime

date_str="2024-06-15"

date_obj=
datetime.strptime(date_str,"%y-%m-%d")

new_date_str=
date_obj,strftime("%d/%m/%y")

print("original date string:",date_str)
print("formatted date string:",new_date_str)

 

output:

original date string:2024-06-15

formatted date string:15/06/2024

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top