Convert any Dates in Spreadsheets using Python
Last Updated :
07 Jan, 2022
In this article, we are going to see how to convert any Dates in Spreadsheets using Python.
Used file:
This file comprises a single column entitled 'Date' and stores random dates of 2021 in some different forms of format.
Approach:
- We'll begin by importing the pandas library.
- Let's have a look at the code that converts the dates.
sample_dates["Date"] = pd.to_datetime(sample_dates["Date"]).dt.strftime("%Y-%m-%d")
- To perform tasks involving date and time, we must first transform the column into a datetime data type, which this section of the code will do:
sample_dates["Date"] = pd.to_datetime(sample_dates["Date"])
- Then we use the dt and strftime methods with a value of "%Y-%m-%d" to inform Python how to format the date. Example here we used is "%Y-%m-%" where %Y is the full year, %m is the month with 2 digits and %d is the date with 2 digits.
Example 1: Convert any Dates in Spreadsheets
Python3
# This code converts any dates in spreadsheet
import pandas as pd
# Read the file and specify which column is the date
sample_dates = pd.read_excel("sample_dates.xlsx")
# Export output with dates converted to YYYY-MM-DD
sample_dates["Date"] = pd.to_datetime(
sample_dates["Date"]).dt.strftime("%Y-%m-%d")
sample_dates.to_excel("sample_dates_formated.xlsx")
Output:

Other well-known formats:
Example date - Saturday, 18 December, 2021, 7:00 PM
- "%A, %B %d" -> “Saturday, December 18”
- "%d-%b-%y" -> “18-Dec-21”
- "%d/%m/%Y" -> "18/12/2021"
- "%b %d, %Y" -> "Dec 18, 2021"
Directive | Meaning | Example |
---|
%a | Weekday as locale's abbreviated name |
Sun, Mon,.....,Sat(en_US);
So, Mo,.....,Sa(de_DE)
|
%A | Weekday as locale full name | Sunday, Monday, ....., Saturday |
%w | Weekday as a decimal number, where 0 is Sunday and 6 is Saturday | 0, 1, 2, 3......,6 |
%d | Day of the month as a zero-padded decimal number | 01,02,....31 |
Example 2:
We're going to use the same dataset that we used in the last example.
Format - "%d %b, %Y" -> "18 December, 2021"
Here we will use '%#d' to remove the padding of zero from the day, i.e. 08 to 8. This will not do padding of zero if the date is in single digit. We can use '%-d' on Linux.
Python3
# This code converts any dates in spreadsheet
import pandas as pd
# Read the file and specify which column is
# the date
sample_dates = pd.read_excel("sample_dates.xlsx")
# Export output with dates converted to
# "D MMMM, YYYY"
sample_dates["Date"] = pd.to_datetime(
sample_dates["Date"]).dt.strftime("%#d %B, %Y")
sample_dates.to_excel("sample_dates_formated.xlsx")
Output:

Example 3:
Here we will use different format
Format - "%B %d, %Y" -> "December 18, 2021"
Python3
# This code converts any dates in spreadsheet
import pandas as pd
# Read the file and specify which column is
# the date
sample_dates = pd.read_excel("sample_dates.xlsx")
# Export output with dates converted to "MMMM D,
# YYYY"
sample_dates["Date"] = pd.to_datetime(
sample_dates["Date"]).dt.strftime("%B %d, %Y")
sample_dates.to_excel("sample_dates_formated.xlsx")
Output:
Similar Reads
Sorting a CSV object by dates in Python CSV stands for comma-separated values. A CSV file can be opened in Google Sheets or Excel and will be formatted as a spreadsheet. However, a CSV file is actually a plain-text file. It can also be opened with a text editor program such as Atom. In this article, we are going to see how to sort a CSV o
2 min read
Converting string into DateTime in Python The goal is to convert a date string like "2021/05/25" into a Python-recognized DateTime object such as 2021-05-25 00:00:00. This enables accurate and consistent date operations like comparisons, calculations and formatting when working with time-related data from sources like files or user input. L
2 min read
Convert Array of Datetimes into Array of Strings in Python In this article, we will convert an array of Datetimes into an array of strings. we have an array whose data type is DateTime and we want to change it to string. The shape and size of the array will be the same as the input array but the data type will be different. We will begin with an introductio
4 min read
Convert string to datetime in Python with timezone Converting a string to a datetime in Python with timezone means parsing a date-time string and creating a timezone-aware datetime object. For example, a string like '2021-09-01 15:27:05.004573 +0530' can be converted to a Python datetime object that accurately represents the date, time and timezone.
2 min read
How to Format date using strftime() in Python ? In this article, we will see how to format date using strftime() in Python. localtime() and gmtime() returns a tuple representing a time and this tuple is converted into a string as specified by the format argument using python time method strftime(). Syntax: time.strftime(format[, sec]) sec: This i
2 min read
Convert Date To Datetime In Python When you're programming, dealing with dates and times is important, and Python provides tools to manage them well. This article is about changing dates into date times in Python. We'll explore methods that can help you switch between these two types of data. Whether you're building a website or work
3 min read