In this article, we gonna show how to convert different input Date formats to a single output date format.
Given Date Formats :
Case 1 : “2019-3-6”
Case 2: “Wed, 6, March, 19”
Case 3: “Wednesday, 6, March, 9”
Case 4: “6-3-2019”
Output date format should be: 3/6/2019
// Importing datetime
// First type of Date and it's format
inputDate0 = "2019-3-6"
DateFormat0 = "%Y-%m-%d"
// Second type of Date and it's format
inputDate1 = "Wed, 6, March, 19"
DateFormat1 = "%a, %d, %B, %y"
// Third type of Date and it's format
inputDate2 = "Wednesday, 6, March, 19"
DateFormat2 = "%A, %d, %B, %y"
// Fourth type of Date and it's format
inputDate3 = ""6-3-2019""
DateFormat3 = "%d-%m-%Y"
// Out date format
outPutDateFormat = "%m/%d/%y"
// Converting input date to Date type object
date0 = datetime.datetime.strptime(inputDate0 , DateFormat0 )
date1 = datetime.datetime.strptime(inputDate1 , DateFormat1 )
date2 = datetime.datetime.strptime(inputDate2 , DateFormat2 )
date3 = datetime.datetime.strptime(inputDate3 , DateFormat3 )
// Print different combination of dates as per output format
print datetime.date.strftime(date0, outPutDateFormat )
print datetime.date.strftime(date1, outPutDateFormat )
print datetime.date.strftime(date2, outPutDateFormat )
print datetime.date.strftime(date3, outPutDateFormat )
Output will be like this
Please check this official Python’s documentation for more info:
You may Also Like
/ January 6, 2021
In Python, there is a module, which allows you to copy a mutable object so that changes made in the...
/ December 15, 2020
What is Unity? According to Wikipedia : Unity is a cross-platform game engine with a built-in IDE developed by Unity...
/ December 8, 2020
Cascading Style Sheets, fondly referred to as CSS, is a style sheet language used to define visual appearance and formatting...
/ December 1, 2020
Mobile applications have come a long way since the basic arcade game “Snake,” which many consider the first mobile application....
/ November 23, 2020
In this tutorial we gonna learn how to create a simple login form using HTML and CSS Step 1) Add...
/ November 16, 2020
Information technology is growing rapidly and it is not slowing down, neither it will according to the pace and the...
/ November 9, 2020
What is PHP ? PHP is a server-side scripting language that is used to develop Static websites or Dynamic websites...
/ November 6, 2020
Machine Learning is undeniably one of the most influential and powerful technologies in today’s world. Machine Learning is enabling computers...
/ November 2, 2020
A modal is a dialog box/popup window that is displayed on top of the current page: How To Create a...
/ October 29, 2020
Blockchain is a technology that is rapidly expanding with the development of new platforms and applications. Blockchain is an open...