Join WhatsApp ChannelJoin Now

Python Change Date Format Example

This post was last updated on July 31st, 2021 at 04:23 am

Hi Dev,

Today, i we will show you python change date format example. This article will give you simple example of python change date format example. you will learn python change date format example. So let’s follow few step to create example of python change date format 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

Recommended Posts