Skip to content

All topics  /  Python

How To Change Date Format In Python?

Python

Development teams using “How To Change Date Format In Python?” in client or internal work can use the related planning guide to document project effort and hand-offs, then assess the record against delivered functionality, tests and agreed outcomes.

Validate current syntax, security guidance and supported versions through Python.org before using the example in production.

Jul 26, 2021

Hello Friends,

Now let's see example of how to convert date formats in python. I would like to share with you how to change date format using python. We will show python change date format example.

In this example, I am going to show you python convert date format example. This article will give you change date format using python.

Here i will give you example for how to change date format in python. So let's see below example:

Example 1


import datetime
def try_parsing_date(text):
    for fmt in ('%H:%M %p','%d-%b-%y','%y-%m-%d','%Y-%m-%d','%d-%m-%y %H:%M:%S','%d-%m-%Y %H:%M:%S','%d/%m/%y','%d/%m/%Y','%y/%m/%d %H:%M:%S','%Y/%m/%d %H:%M:%S','%H:%M:%S %p','%H:%M:%S %p','%d/%m/%Y %H:%M %p','%d/%m/%Y %H:%M %p','%y-%m-%d %H:%M:%S','%Y-%m-%d %H:%M:%S'):
        try:
            return datetime.datetime.strptime(text, fmt)
        except ValueError:
            pass
    raise ValueError(text+' no valid date format found')
myDate = '2021-07-02 11:12:13'
myDate = try_parsing_date(myDate)
myDate = myDate.strftime('%Y-%m-%d')
print(myDate)

Output:

02-07-2021

Example 2

import datetime
def try_parsing_date(text):
    for fmt in ('%H:%M %p','%d-%b-%y','%y-%m-%d','%Y-%m-%d','%d-%m-%y %H:%M:%S','%d-%m-%Y %H:%M:%S','%d/%m/%y','%d/%m/%Y','%y/%m/%d %H:%M:%S','%Y/%m/%d %H:%M:%S','%H:%M:%S %p','%H:%M:%S %p','%d/%m/%Y %H:%M %p','%d/%m/%Y %H:%M %p','%y-%m-%d %H:%M:%S','%Y-%m-%d %H:%M:%S'):
        try:
            return datetime.datetime.strptime(text, fmt)
        except ValueError:
            pass
    raise ValueError(text+' no valid date format found')
myDate = '2021-07-02 11:12:13'
myDate = try_parsing_date(myDate)
myDate = myDate.strftime('%H:%M:%S')
print(myDate)

Output:

11:12:13

Example 3

import datetime
def try_parsing_date(text):
    for fmt in ('%H:%M %p','%d-%b-%y','%Y-%m-%d','%d-%m-%y %H:%M:%S','%d-%m-%Y %H:%M:%S','%d/%m/%y','%d/%m/%Y','%y/%m/%d %H:%M:%S','%Y/%m/%d %H:%M:%S','%H:%M:%S %p','%H:%M:%S %p','%d/%m/%Y %H:%M %p','%d/%m/%Y %H:%M %p','%Y-%m-%d %H:%M:%S'):
        try:
            return datetime.datetime.strptime(text, fmt)
        except ValueError:
            pass
    raise ValueError(text+' no valid date format found')
myDate = '2021-07-02 11:12:13'
myDate = try_parsing_date(myDate)
myDate = myDate.strftime('%d %b %y')
print(myDate)

Output:

02 Jul 21

Example 4

import datetime
def try_parsing_date(text):
    for fmt in ('%H:%M %p','%d-%b-%y','%Y-%m-%d','%d-%m-%y %H:%M:%S','%d-%m-%Y %H:%M:%S','%d/%m/%y','%d/%m/%Y','%y/%m/%d %H:%M:%S','%Y/%m/%d %H:%M:%S','%H:%M:%S %p','%H:%M:%S %p','%d/%m/%Y %H:%M %p','%d/%m/%Y %H:%M %p','%Y-%m-%d %H:%M:%S'):
        try:
            return datetime.datetime.strptime(text, fmt)
        except ValueError:
            pass
    raise ValueError(text+' no valid date format found')
myDate = '2021-07-02 11:12:13'
myDate = try_parsing_date(myDate)
myDate = myDate.strftime('%d %B %Y')
print(myDate)

Output:

02 July 2021

Example 5

import datetime
def try_parsing_date(text):
    for fmt in ('%H:%M %p','%d-%b-%y','%y-%m-%d','%Y-%m-%d','%d-%m-%y %H:%M:%S','%d-%m-%Y %H:%M:%S','%d/%m/%y','%d/%m/%Y','%y/%m/%d %H:%M:%S','%Y/%m/%d %H:%M:%S','%H:%M:%S %p','%H:%M:%S %p','%d/%m/%Y %H:%M %p','%d/%m/%Y %H:%M %p','%y-%m-%d %H:%M:%S','%Y-%m-%d %H:%M:%S'):
        try:
            return datetime.datetime.strptime(text, fmt)
        except ValueError:
            pass
    raise ValueError(text+' no valid date format found')
myDate = '2021-07-02 11:12:13'
myDate = try_parsing_date(myDate)
myDate = myDate.strftime('%H:%M:%S %p')
print(myDate)

Output:

11:12:13 AM

It will help you....