How To Change Date Format In Excel, Python, Javascript, And Sql

You Have the Date, But It Looks Wrong

You just pulled a report, and the dates are a jumbled mess. Maybe they show as “2025-01-15” when you need “01/15/2025.” Or perhaps your Python script is throwing an error because it can’t read “15-Jan-2025.” This mismatch isn’t just an aesthetic issue; it can break formulas, halt data imports, and cause reports to sort incorrectly.

Changing a date’s format is one of the most common yet frustrating tasks in data work. The date information is often correct underneath, but its presentation doesn’t fit your system’s needs or your region’s standards. Whether you’re cleaning data in a spreadsheet, parsing it in code, or querying a database, knowing how to transform date formats is an essential skill.

This guide walks through the practical, step-by-step methods for changing date formats in the tools you use every day: Microsoft Excel, Python, JavaScript, and SQL. We’ll cover the core concepts, provide clear code examples, and highlight the common pitfalls that trip people up.

Understanding What a Date Format Actually Is

Before changing anything, it’s crucial to distinguish between a date’s *value* and its *format*. The value is the underlying data point—the specific moment in time. The format is merely how that value is displayed to you.

Think of it like a number. The value 1000.5 can be displayed as “1,000.50”, “$1,000.50”, or “1.000,50€” depending on locale and formatting rules. The number itself hasn’t changed. Dates work the same way. The internal value for January 15, 2025, remains constant. Whether you see it as “15/01/25”, “2025-01-15”, or “January 15, 2025” is purely a matter of formatting.

Most problems occur when a program misinterprets a text string like “03/04/2025” as a date. Is that March 4th or April 3rd? The format dictates the interpretation. Changing the format correctly ensures both humans and machines understand the date the same way.

Common Date Format Symbols

Across different systems, you’ll see placeholders that define the format.

– YYYY or yyyy: Four-digit year (2025)

– YY or yy: Two-digit year (25)

– MM or mm: Two-digit month (01 for January)

– MMM: Abbreviated month name (Jan)

– MMMM: Full month name (January)

– DD or dd: Two-digit day of the month (15)

– d: Day of the month without leading zero (1, 15)

– HH or hh: Two-digit hour (24-hour or 12-hour clock, context-dependent)

– mm: Two-digit minute (when used with hours)

– ss: Two-digit seconds

Knowing these symbols is your key to constructing any custom format you need.

Changing Date Format in Microsoft Excel and Google Sheets

Spreadsheets are where most people first encounter date format issues. A column of dates might import as text, display in an unfamiliar order, or simply refuse to sort.

Using Built-in Format Options

Excel and Google Sheets provide quick, menu-driven formatting. Select your date cells, then find the number format dropdown in the toolbar (often labeled “Number” or with a small number icon).

– In Excel: Home tab > Number group > Dropdown. Choose “Short Date” (e.g., 1/15/2025) or “Long Date” (e.g., Monday, January 15, 2025).

– In Google Sheets: Format > Number > Date, Date time, or a similar option.

how to change format of date

This applies a standard format. If the dates are already proper date values, this change is instantaneous.

Creating a Custom Date Format

When the built-in options don’t fit, you need a custom format. In Excel, right-click the cells, select “Format Cells,” and go to the Number tab. Choose “Custom” from the category list.

In the “Type” box, you can build your format using the symbols. For example:

– “mm/dd/yyyy” displays as 01/15/2025

– “dd-mmm-yy” displays as 15-Jan-25

– “yyyy-mm-dd” displays as 2025-01-15 (ISO standard)

– “dddd, mmmm d, yyyy” displays as Wednesday, January 15, 2025

Type your desired pattern and click OK. The display updates, but the underlying value is untouched. Google Sheets has a similar “Custom date and time” option under Format > Number > Custom number format.

Fixing Text Dates with DATEVALUE and TEXT

If your “dates” are actually text strings, formatting won’t work. You must first convert them to true date values.

Use the DATEVALUE function. If cell A1 has the text “1/15/2025”, the formula `=DATEVALUE(A1)` returns the Excel date serial number for that date. You can then format that result.

Conversely, to convert a true date into a specific text format, use the TEXT function. `=TEXT(A1, “mm/dd/yyyy”)` will output the text string “01/15/2025”. This is useful for creating date strings for use in other systems or reports, but the result is no longer a calculable date.

Changing Date Format in Python

In Python, the `datetime` module is your primary tool for handling dates. The process typically involves two steps: parsing a string into a datetime object, then formatting that object back into a new string.

Parsing Strings with strptime

The `strptime` method (string parse time) converts a formatted string into a datetime object. You must tell it the exact format of your input string.

from datetime import datetime

date_string = "15-01-2025"
# Define the format to match the string: Day-Month-Year
date_object = datetime.strptime(date_string, "%d-%m-%Y")
print(date_object)  # Output: 2025-01-15 00:00:00

If your string is “2025/01/15”, you would use the format `”%Y/%m/%d”`. A mismatch between the string and format code will raise a `ValueError`.

Formatting Objects with strftime

Once you have a datetime object, use `strftime` (string format time) to output it in any format.

from datetime import datetime

date_object = datetime(2025, 1, 15)
# Format the object into a new string
new_format = date_object.strftime("%A, %B %d, %Y")
print(new_format)  # Output: Wednesday, January 15, 2025

another_format = date_object.strftime("%Y%m%d")
print(another_format)  # Output: 20250115

Common format codes here differ slightly from spreadsheet symbols: `%Y` (4-digit year), `%m` (2-digit month), `%d` (2-digit day), `%B` (full month name), `%b` (abbreviated month), `%A` (full weekday name).

Using dateutil for Flexible Parsing

For messy, real-world data where the format isn’t consistent, the third-party `dateutil` parser can be a lifesaver.

from dateutil import parser

date_string1 = "Jan 15, 2025"
date_string2 = "15-1-25"
date_string3 = "20250115"

date1 = parser.parse(date_string1)
date2 = parser.parse(date_string2)
date3 = parser.parse(date_string3)

print(date1.strftime("%Y-%m-%d"))  # All output: 2025-01-15

It intelligently guesses the format, though it’s slower and less explicit than `strptime`.

Changing Date Format in JavaScript

JavaScript’s `Date` object is notoriously tricky. It has limited built-in formatting methods, so developers often rely on the `Intl.DateTimeFormat` object or libraries like `date-fns`.

Using Intl.DateTimeFormat

The `Intl` API provides locale-aware formatting. You define options for how you want the date to appear.

const date = new Date('2025-01-15');

// Format for US English
const usFormatter = new Intl.DateTimeFormat('en-US', {
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  weekday: 'long'
});
console.log(usFormatter.format(date));
// Output: Wednesday, January 15, 2025

// Format for a numeric ISO-like format
const isoFormatter = new Intl.DateTimeFormat('en-CA', {
  year: 'numeric',
  month: '2-digit',
  day: '2-digit'
});
console.log(isoFormatter.format(date).replace(/\//g, '-'));
// Output: 2025-01-15 (en-CA uses YYYY-MM-DD)

This method is powerful for internationalization but can be verbose for simple changes.

how to change format of date

Manual Formatting with Getter Methods

For full control, you can build the format string manually using the Date object’s getter methods.

const date = new Date('2025-01-15');

const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are 0-indexed
const day = String(date.getDate()).padStart(2, '0');

const yyyymmdd = `${year}-${month}-${day}`; // 2025-01-15
const mmddyyyy = `${month}/${day}/${year}`; // 01/15/2025

console.log(yyyymmdd, mmddyyyy);

This approach is clear and unambiguous, making it a good choice for simple, project-specific formatting.

Changing Date Format in SQL

SQL is used to transform date formats directly within database queries. The functions differ by database system (MySQL, PostgreSQL, SQL Server), but the concepts are similar.

Formatting Dates in MySQL

Use the `DATE_FORMAT()` function to convert a date column into a formatted string.

SELECT
  order_date,
  DATE_FORMAT(order_date, '%W, %M %d, %Y') AS formatted_date,
  DATE_FORMAT(order_date, '%Y%m%d') AS compact_date
FROM orders;

This would return the original date, a long version like “Wednesday, January 15, 2025”, and a compact version “20250115”.

Formatting Dates in PostgreSQL

PostgreSQL uses the `TO_CHAR()` function for formatting.

SELECT
  order_date,
  TO_CHAR(order_date, 'Day, Month DD, YYYY') AS formatted_date,
  TO_CHAR(order_date, 'YYYYMMDD') AS compact_date
FROM orders;

The pattern syntax is slightly different, using more descriptive words for the format codes.

Formatting Dates in SQL Server

SQL Server employs the `CONVERT()` or `FORMAT()` functions. `CONVERT()` uses style codes.

SELECT
  order_date,
  CONVERT(VARCHAR, order_date, 101) AS us_format, -- mm/dd/yyyy
  CONVERT(VARCHAR, order_date, 112) AS iso_format -- yyyymmdd
FROM orders;

The `FORMAT()` function (available in newer versions) is more flexible, similar to .NET formatting strings: `FORMAT(order_date, ‘yyyy-MM-dd’)`.

When Your Date Change Doesn’t Work

You’ve followed the steps, but the dates are still wrong, sorting incorrectly, or causing errors. Here are the usual suspects.

Locales and Regional Settings

The biggest culprit is locale. Your operating system, spreadsheet, or application has a default locale that dictates the expected date order (MM/DD/YYYY vs DD/MM/YYYY). A Python script written in the US might fail on a German server because `%m/%d/%Y` doesn’t match the input data’s format.

Always be explicit about locales when parsing. In Python’s `strptime`, the format string overrides the locale. In JavaScript’s `Intl.DateTimeFormat`, you set the locale as the first argument. In Excel, check your Windows or Mac regional settings, as they influence default interpretation.

Leading Zeros and Two-Digit Years

Missing leading zeros can cause parsing failures. “1/5/2025” is ambiguous. Should it be parsed with `%m/%d/%Y` or `%d/%m/%Y`? Always use `%m` and `%d` for two-digit months and days when parsing, and pad your strings if necessary.

Two-digit years are a legacy headache. Systems interpret “25” as 2025 or 1925 based on a “pivot year” rule. Whenever possible, use and store four-digit years to avoid century ambiguity.

Dates Stored as Text

As mentioned, you cannot format text. In Excel, text dates are often left-aligned in the cell, while true dates are right-aligned. Use `ISTEXT()` or `ISNUMBER()` to check. Conversion is required before formatting. In SQL, if a date column has a `VARCHAR` type, you must first `CAST` or `CONVERT` it to a date type before applying date functions.

Choosing the Right Strategy for Your Task

With so many methods, which one should you use? It depends on your goal.

For a one-time data cleanup in a spreadsheet, use Excel’s Text to Columns wizard or the DATEVALUE/TEXT functions. It’s visual and immediate.

For an automated data processing script in Python, use `datetime.strptime` and `strftime` with explicit format strings. It’s precise and reproducible.

For a web application frontend in JavaScript, use `Intl.DateTimeFormat` to respect the user’s browser locale, ensuring a good international user experience.

For database reporting in SQL, format the dates directly in your query using the database’s native functions. This keeps the logic close to the data and can improve performance over formatting in application code.

The consistent theme is to transform your date into a proper, system-recognized date object or value first. Once it’s in that standard internal form, displaying it in any format becomes a straightforward, reversible operation. Master this two-step process—parse, then format—and you’ll have control over dates in any environment.

Leave a Comment

close