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
/ September 24, 2020
What is C++ C++ is a programming language developed by Bjarne Stroustrup in 1979 at Bell Labs. Originally, it was...
/ September 9, 2020
It has become highly essential to use a PHP MVC framework to develop custom web applications and there are many...
/ September 2, 2020
In the world of programming languages, there is no respite for breakthroughs and innovations. The developers are always in search...
/ June 16, 2020
What is Python? Python is an interpreted, high-level, general-purpose programming language. It was created by Guido van Rossum, and released...
/ May 18, 2020
LAMP stands for Linux, Apache, MySQL, and PHP, Perl, or Python. It offers a great alternative to commercial software packages. ...
/ May 7, 2020
Java is an object-oriented language similar to C++, but with advanced and simplified features. Java is free to access and...
/ April 29, 2020
Cascading Style Sheets, fondly referred to as CSS, is a style sheet language used to define visual appearance and formatting...
/ April 23, 2020
First developed by Tim Berners-Lee in 1990, HTML is short for Hypertext Markup Language. HTML is a computer language devised...
/ April 17, 2020
Django is an MVT web framework used to build web applications. Django is an excellent web framework for Python that...
/ April 14, 2020
As one of the first PHP frameworks created, CakePHP is among the most popular PHP frameworks today. CakePHP, being a...