[Python] How to change the date format (display format)

How to change the date format (display format) in python

When the date type is output as a character string, the default is "yyyy-mm-dd". Change this to any format, such as "yyyy / mm / dd" or "yyyy year m month d day".

  • The list format is "yyyy-mm-dd". Only strings can be reformatted.
Default display example of date
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/563526/d00baad3-2a02-517c-a5f2-ff46446928ea.png)
`2020-03-22` (string | str)

table of contents

  1. ["Two" ways to change the date](#Two ways to change the date)
  2. [[0 padded] date format change (example: yyyy / mm / dd)](# 1 date format change example yyyymmdd)
  3. [(1-1) Change with strptime method](# 1-1 Change with strptime method)
  4. [Change with (1-2) format method](Change with # 1-2 format method)
  5. [[0 padded] date format change (example: yyyy year m month d day)](# 2 date format change example yyyy year m month d day)
  6. [(2-1) Change with strptime method](# 2-1 Change with strptime method)
  7. [Change with (2-2) format method](Change with # 2-2 format method)
  8. [Use (2-3) year, month, day methods](use # 2-3 year-month-day methods)
  9. [Use (2-4) replace method](Use # 2-4 replace method)

"Two" ways to change the date

** ① Use strptime method ** ** ② Use format method **


## [1] Change date format (example: yyyy / mm / dd) 0 padded pattern

(1-1) Change with strptime method

.strftime('%Y/%m/%d') └ Example: "today.strftime ("% Y /% m /% d ")" └ "% Y": Decimal number in the Christian era (4 digits) (% y is 2 digits) └ "% m": Decimal month filled with 0 (% M is minutes) └ "% d": 0-filled decimal date (% D is mm / dd / yy)

Source code


import datetime as dt
today = dt.date.today()  #Output: datetime.date(2020, 3, 22)

today.strftime("%Y/%m/%d")

#Output result
#'2020/03/22'

Type is a string 「type(today.strftime("%Y/%m/%d"))」:str


### (1-2) Change with format method `'{0:%Y/%m/%d}'.format()` └ Example:'{0:% Y /% m /% d}'. format (today) └ "0:" Specify which element to apply to (* cannot be omitted) * In the above example, there is only one today

Source code


import datetime as dt
today = dt.date.today()  #Output: datetime.date(2020, 3, 22)

'{0:%Y/%m/%d}'.format(today)

#Output result
#'2020/03/22'

Type is a string 「type('{0:%Y/%m/%d}'.format(today))」:str


** ■ Application example of index number ** `'{0:%Y/%m/%d}'.format()` └ When specifying a value other than 0 with "0:"

-Used when there are multiple dates in the argument of format ()

Example of using index number


#Create 3 dates
today = dt.date.today()
past = dt.date(2015,1,1)
future = dt.date(2030,1,1)

#Change index number (0)~2)

'{0:%Y/%m/%d}'.format(today,past,future)
#output:'2020/03/22'

'{1:%Y/%m/%d}'.format(today,past,future)
#output:'2015/01/01'

'{2:%Y/%m/%d}'.format(today,past,future)
#output:'2030/01/01'

## [2] Change date format (example: yyyy year m month d day) 0 unfilled pattern

(2-1) Change with strptime method

.strftime('%Y/%#m/%#d') └ Example: "today.strftime ("% Y /% # m /% # d ")" └ "% Y": Decimal number in the Christian era (4 digits) (% y is 2 digits) └ "% # m": 0 unfilled decimal month (% M is minutes) └ "% # d": 0 unfilled decimal date (% D is mm / dd / yy)

** ▼ 0 How to make it unfilled **

Source code(windows)


today = dt.date.today() 
today.strftime("%Y year%#m month%#d day")

#Output result
#'March 22, 2020

### (2-2) Change with format method `'{0:% Y year% # m month% # d day}'. format ()` └ Example:'{0:% Y year% # m month% # d day}'. format (today) └ "0:" Specify which element to apply to (* cannot be omitted)

Source code


today = dt.date.today() 
'{0:%Y year%#m month%#d day}'.format(today)

#Output result
#'March 22, 2020

### (2-3) Use year, month, day methods `str (today.year) +" year "+ str (today.month) +" month "+ str (today.day) +" day "` └ ".year": 4 digits in the Christian era (type: int) └ ".month": 0 unfilled month (type: int) └ ".day": 0 unfilled date (type: int) └ "+": int and str cannot be connected ⇒ convert int to str

Source code


today = dt.date.today() 
str(today.year)+"Year"+str(today.month)+"Month"+str(today.day)+"Day"

#Output result
#'March 22, 2020

### (2-4) Use replace method `y = today.strftime ("% Y year ")` `md = today.strftime ("% m month% d day "). replace ("0 "," ")` `y+md`

Source code


today = dt.date.today() 
y = today.strftime("%Y year")
md = today.strftime("%m month%d day").replace("0","")
y+md

#Output result
#'March 22, 2020

I think there are many other ways, but once it's like this. Please let me know if there is a good way.

[Back to top](How to change the date format display format with #python)

Recommended Posts

[Python] How to change the date format (display format)
How to change Python version
How to get the Python version
[Python] How to import the library
How to calculate date with python
[Python] Change the alphabet to numbers
How to set the extended iso8601 format date to the Dataframe index
How to display the progress bar (tqdm)
[Python] How to display random numbers (random module)
How to display multiplication table in python
How to display python Japanese with lolipop
Convert Python date types to RFC822 format
How to display Hello world in python
Determine the date and time format in Python and convert to Unixtime
How to change the log level of Azure SDK for Python
[Python] Explains how to use the format function with an example
How to display bytes in the same way in Java and Python
[python] How to display list elements side by side
How to use the C library in Python
[Python] How to specify the window display position and size of matplotlib
How to install Python
[Python] How to change character string (str) data to date (strptime of datetime)
How to get the date and time difference in seconds with python
How to install python
[Introduction to Python] How to handle JSON format data
[Algorithm x Python] How to use the list
How to erase the characters output by Python
How to get the files in the [Python] folder
[Introduction to Python] How to write a character string with the format function
[Python] Split the date
I want to display the progress in Python!
Change the Key of Object on S3 from normal date format to Hive format
[Python] How to remove duplicate values from the list
How to retrieve the nth largest value in Python
How to get the variable name itself in python
Think about how to program Python on the iPad
[Introduction to Python] How to iterate with the range function?
How to know the current directory in Python in Blender
[Reintroduction to python] How to import via the parent directory
How to use the Raspberry Pi relay module Python
[Python] How to specify the download location with youtube-dl
[Python] How to use the graph creation library Altair
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set
[Python] Summary of how to specify the color of the figure
[Work efficiency] How to change file names in Python
How to use the model learned in Lobe in Python
[Introduction to Python] How to stop the loop using break?
[Python] How to rewrite the table style with python-pptx [python-pptx]
How to enjoy Python on Android !! Programming on the go !!
python> datetime> From date string (ISO format: 2015-12-09 12:40:08) to datetime type
[Python] How to output the list values in order
Python OpenCV tried to display the image in text.
[2020.8 latest] How to install Python
[python] Convert date to string
python3: How to use bottle (2)
How to use the generator
[Python] How to use list 1
How to change Jupyter layout
How to update Python Tkinter to 8.6
How to use Python argparse