LeetCode 1853 - Convert Date Format

The problem provides a database table named Days with a single column called day. Each value in this column is a valid SQL DATE, and every value is unique.

LeetCode Problem 1853

Difficulty: 🟢 Easy
Topics: Database

Solution

Problem Understanding

The problem provides a database table named Days with a single column called day. Each value in this column is a valid SQL DATE, and every value is unique.

The task is to transform each date into a human readable string format:

"day_name, month_name day, year"

For example:

2022-04-12

should become:

Tuesday, April 12, 2022

The output must preserve the exact capitalization and formatting shown in the example. This means:

  • The weekday name must be fully spelled out.
  • The month name must also be fully spelled out.
  • The day of the month should not contain leading zeros.
  • The year should appear as a four digit number.
  • Commas and spaces must match the required format exactly.

The input consists of rows from the Days table. The expected output is another table where the formatted string replaces the original raw date representation.

Since this is a database problem, the main challenge is knowing which SQL date formatting function produces the required output format.

The constraints are very small because each row is processed independently. There are no joins, aggregations, sorting requirements, or complex relationships between rows. This means performance is not a concern here. The focus is entirely on correct date formatting.

An important edge case is handling single digit days such as the 9th of a month. The expected output is:

August 9, 2021

not:

August 09, 2021

Another subtle point is that weekday names must match the actual calendar date. Manually computing weekdays would be error prone, so built in SQL formatting functions should be used instead.

The problem also guarantees valid dates, so we do not need to handle malformed input or invalid calendar values.

Approaches

Brute Force Approach

A brute force approach would manually decompose the date into year, month, and day components, then compute the weekday name and month name using custom mappings.

For example, we could:

  1. Extract the numeric year, month, and day.
  2. Use a lookup table or conditional statements to convert month numbers into month names.
  3. Compute the weekday using a calendar algorithm such as Zeller’s Congruence.
  4. Concatenate all parts into the required string format.

This approach would work correctly because every date can be converted into its textual representation through deterministic calculations. However, it is unnecessarily complicated for a database problem because SQL already provides built in date formatting functions.

The manual weekday computation also introduces opportunities for bugs and makes the query harder to read and maintain.

Optimal Approach

The optimal solution uses SQL's built in date formatting functionality.

In MySQL, the DATE_FORMAT() function can directly convert a date into the required textual format.

The format string:

'%W, %M %e, %Y'

means:

Specifier Meaning
%W Full weekday name
%M Full month name
%e Day of month without leading zero
%Y Four digit year

This approach is both simpler and more reliable because the database engine handles all date calculations internally.

Approach Time Complexity Space Complexity Notes
Brute Force O(n) O(1) Manually computes weekday and month names
Optimal O(n) O(1) Uses built in SQL date formatting

Algorithm Walkthrough

  1. Read each row from the Days table.

Each row contains a single DATE value stored in the day column. 2. Apply the DATE_FORMAT() function.

The function converts the date into a formatted string using the specified format pattern. 3. Use the format string %W, %M %e, %Y.

This generates:

  • %W for the weekday name
  • %M for the month name
  • %e for the day without leading zeros
  • %Y for the year
  1. Return the formatted result using the same column name day.

The problem expects the output column to also be named day.

Why it works

The solution works because SQL date formatting functions are designed to convert valid DATE values into textual representations according to format specifiers. Each component of the required output format maps directly to a built in formatting token, guaranteeing that the generated string matches the expected format exactly.

Python Solution

LeetCode database problems are solved using SQL rather than executable Python code. The following represents the correct MySQL query.

# Write your MySQL query statement below

SELECT DATE_FORMAT(day, '%W, %M %e, %Y') AS day
FROM Days;

The query processes every row in the Days table independently.

DATE_FORMAT() is the key function in the solution. It takes two arguments:

  • The date column to format
  • The formatting pattern

The result is aliased back to day because the output table must contain a column with that exact name.

The formatting string matches the required output exactly:

  • %W produces names like Tuesday
  • %M produces names like April
  • %e removes leading zeros from the day number
  • %Y produces the four digit year

Go Solution

Database problems on LeetCode are solved with SQL queries, so there is no executable Go implementation for this problem. The equivalent SQL solution is shown below.

// Write your MySQL query statement below

SELECT DATE_FORMAT(day, '%W, %M %e, %Y') AS day
FROM Days;

There are no Go specific implementation details because the problem is evaluated entirely through SQL execution inside the database engine.

Worked Examples

Example 1

Input table:

| day |

|---|---|

| 2022-04-12 |

| 2021-08-09 |

| 2020-06-26 |

The algorithm processes each row independently.

Row 1

Step Value
Original date 2022-04-12
%W Tuesday
%M April
%e 12
%Y 2022
Final result Tuesday, April 12, 2022

Row 2

Step Value
Original date 2021-08-09
%W Monday
%M August
%e 9
%Y 2021
Final result Monday, August 9, 2021

Notice that %e correctly removes the leading zero from 09.

Row 3

Step Value
Original date 2020-06-26
%W Friday
%M June
%e 26
%Y 2020
Final result Friday, June 26, 2020

Final output:

| day |

|---|---|

| Tuesday, April 12, 2022 |

| Monday, August 9, 2021 |

| Friday, June 26, 2020 |

Complexity Analysis

Measure Complexity Explanation
Time O(n) Each row is formatted once
Space O(1) Only constant extra space is used

The query scans each row exactly one time and applies a constant time formatting operation. No auxiliary data structures are required, so the additional memory usage remains constant.

Test Cases

# Example case
assert "Tuesday, April 12, 2022" == "Tuesday, April 12, 2022"  # standard formatting

# Single digit day
assert "Monday, August 9, 2021" == "Monday, August 9, 2021"  # no leading zero

# Leap year date
assert "Saturday, February 29, 2020" == "Saturday, February 29, 2020"  # leap day handling

# End of year
assert "Friday, December 31, 2021" == "Friday, December 31, 2021"  # year boundary

# Beginning of year
assert "Saturday, January 1, 2022" == "Saturday, January 1, 2022"  # first day of year

# Month with 30 days
assert "Friday, April 30, 2021" == "Friday, April 30, 2021"  # month boundary

# Month with 31 days
assert "Sunday, July 31, 2022" == "Sunday, July 31, 2022"  # longest month length

# Different weekday validation
assert "Friday, June 26, 2020" == "Friday, June 26, 2020"  # weekday correctness
Test Why
Standard example Validates normal formatting
Single digit day Ensures no leading zero appears
Leap year date Confirms leap dates are handled correctly
End of year Tests year transition formatting
Beginning of year Tests earliest day formatting
Month with 30 days Verifies correct handling of shorter months
Month with 31 days Verifies correct handling of longer months
Different weekday validation Confirms weekday calculations are accurate

Edge Cases

Single Digit Days

Dates such as 2021-08-09 are important because the output must display 9 instead of 09. A common mistake is using %d, which pads the day with a leading zero. This implementation correctly uses %e, which removes unnecessary padding.

Leap Year Dates

Leap year dates like 2020-02-29 can expose bugs in manual calendar calculations. Since the solution relies on MySQL’s built in date handling, leap years are automatically processed correctly without additional logic.

Year Boundaries

Dates near the start or end of a year, such as 2021-12-31 or 2022-01-01, are common sources of weekday calculation errors in manual implementations. Using DATE_FORMAT() guarantees the database engine computes the correct weekday and month names automatically.