• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Date Format YYYYMMDD in Python

Date Format YYYYMMDD in Python

March 12, 2022 Leave a Comment

In Python, we can easily format dates and datetime objects with the strftime() function. For example, to format a date as YYYY-MM-DD, pass “%Y-%m-%d” to strftime().

import datetime 

currentDate = datetime.date.today()

print(currentDate.strftime("%Y-%m-%d"))

#Output:
2022-03-12

If you want to create a string that is separated by slashes (“/”) instead of dashes (“-“), pass “%Y/%m/%d” to strftime().

import datetime 

currentDate = datetime.date.today()

print(currentDate.strftime("%Y/%m/%d"))

#Output:
2022/03/12

When working in Python, many times we need to create variables which represent dates and times. When creating and displaying values related to dates, sometimes we need to display the current date in a particular format.

The Python strftime() function is very useful when working with date and datetime variables. strftime() accepts a string representing the format of a date and returns the date as a string in the given format.

We can use strftime() to easily format a date using codes for the day, month or year.

For example, if we want to format a date in the format yyyymmdd, we can do so by passing “%Y” for year, “%m” for month, and “%d” for day to strftime() in the order you want.

Below is a simple example of how to format and print a date with the format yyyy-mm-dd in Python with strftime().

import datetime 

currentDate = datetime.date.today()

print(currentDate.strftime("%Y-%m-%d"))

#Output:
2022-03-12

How to Change Date Formats in Python with strftime()

We can use strftime() to format dates and change the date format easily of a date or datetime object.

For example, if you want to change the date format to MM/DD/YYYY, you can pass “%m/%d/%Y” to strftime().

import datetime 

currentDate = datetime.date.today()

print(currentDate.strftime("%m/%d/%Y"))

#Output:
03/12/2022

Another useful code for formatting dates is the month name code “%B”. “%B” gets the month name of a date.

We can format a date like “Month Name DD, YYYY” easily by passing “%B %d, %Y” to strftime().

import datetime 

currentDate = datetime.date.today()

print(currentDate.strftime("%B %d, %Y"))

#Output:
March 12, 2022

Hopefully this article has been useful for you to learn how to learn how to format dates in Python with strftime().

Other Articles You'll Also Like:

  • 1.  How to Count Vowels in a String Using Python
  • 2.  Using Python to Convert Float to Int
  • 3.  nunique pandas – Get Number of Unique Values in DataFrame
  • 4.  Sort Series in pandas with sort_values() Function
  • 5.  How to Rotate a List in Python
  • 6.  Write Variable to File Using Python
  • 7.  Calculate Compound Interest in Python
  • 8.  Using Python To Split String by Comma into List
  • 9.  Python atanh – Find Hyperbolic Arctangent of Number Using math.atanh()
  • 10.  Python tostring method – Convert an Object to String with str() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy