Day 9 - Python

Problem Statement: Date Format Converter

You are given a Python function convert_date(ds: str) -> pd.DataFrame that takes a date string ds as input and converts it into various formats. The function returns a pandas DataFrame containing the converted date strings along with their corresponding formats.

The function uses the datetime.strptime() method to parse the input date string into a datetime object. Then, it formats this datetime object into different date formats specified in the formats list.

Your task is to:

Implement the missing part of the provided function.

Write additional code to take user input for a date string.

Call the convert_date() function with the user input date string.

Print the resulting DataFrame.

Function Signature:

def convert_date(ds: str) -> pd.DataFrame:

Input:

ds (str): A date string in the format 'YYYY-MM-DD'.

Output:

A pandas DataFrame with two columns:

'Format': Contains the description of the date format.

'Result': Contains the converted date string in the respective format.

Example:

Input:

1981-07-07

Output:

                             Format                                              Result

0               Month Day, Year                                            July 07, 1981

1               Day Month Year                                             07 July 1981

2               Day of the Week, Month Day, Year           Tuesday, July 07, 1981

3               YYYY-MM-DD                                               1981-07-07

4               MM/DD/YYYY                                                07/07/1981

5               DD-MM-YYYY                                                07-07-1981

6               Day of the Week, DD Month YYYY             Tuesday, 07 July 1981

Note:

Ensure that the formats list and its corresponding descriptions are correctly implemented.

You can assume that the input date string is always in the format 'YYYY-MM-DD'.


SOLUTION:

import pandas as pd

from datetime import datetime


def convert_date(ds: str) -> pd.DataFrame:

    dateObj = datetime.strptime(ds, "%Y-%m-%d")

    formats = [

        ("%B %d, %Y", "Month Day, Year"),

        ("%d %B %Y", "Day Month Year"),

        ("%A, %B %d, %Y", "Day of the Week, Month Day, Year"),

        ("%Y-%m-%d", "YYYY-MM-DD"),

        ("%m/%d/%Y", "MM/DD/YYYY"),

        ("%d-%m-%Y", "DD-MM-YYYY"),

        ("%A, %d %B %Y", "Day of the Week, DD Month YYYY")

    ]

    

    results = [(desc, dateObj.strftime(fmt)) for fmt, desc in formats]

    df = pd.DataFrame(results, columns=['Format', 'Result'])

    return df


dateStr = input()

result = convert_date(dateStr)

print(result)


Insights:
  • The program utilizes the datetime.strptime() function from the datetime module to convert the input date string into a datetime object.
  • The code defines a list named formats, where each element is a tuple containing a date format string and its corresponding description.
  • For each format-description tuple in the formats list, the code converts the datetime object into the specified format using the strftime() method.
  • Refer https://strftime.org/ for more formats.
  • The results of the format conversions are stored in a list of tuples named results, where each tuple contains the description of the format and the converted date string.
  • The list of results is then used to create a pandas DataFrame named df with two columns: 'Format' and 'Result'. The function returns the pandas DataFrame df containing the format descriptions and their corresponding converted date strings.
  • The code prints the resulting pandas DataFrame, displaying the various date formats along with their corresponding converted date strings.
Happy exploration of Date and Time Formats! 
Happy coDDing!! :)

Comments