Skip to content Skip to sidebar Skip to footer

Python Datetime.strftime Does Not Support Japanese?

I have a problem that following code causes a encoding error: This problem happens when pass a japanese including string to strftime. This is caused on python repl on cmd.exe. Is t

Solution 1:

It works after adding the encoding in your script.

Ex:

# -*- coding: utf-8 -*-

import datetime
d = datetime.datetime.now()
print( d.strftime("%y 年") )

Output:

18 年

Solution 2:

In Python 3.6+, using f-string:

importdatetimed= datetime.datetime.now()
y = d.strftime("%y")
assert f'{y}年' == '18年'

Post a Comment for "Python Datetime.strftime Does Not Support Japanese?"