Three Simple Python Tricks to Convert Chinese Dates to YYYY/MM/DD
This article walks through a fan's question on converting a Chinese date string like '2021年9月28日' into the standard '2021/9/28' format, presenting three practical Python solutions—including direct replacement, split‑and‑join, and datetime parsing—so you can pick the most suitable method for bulk data processing.
Introduction
A fan asked how to turn the Chinese date string 2021年9月28日 into the format 2021/9/28 using Python. Handling many such dates manually is cumbersome, so a programmatic solution is useful.
Idea
Although a simple string replace could work, three robust approaches are demonstrated to cover different scenarios and preferences.
Solution
Method 1
Directly replace the Chinese characters with slashes.
# coding: utf-8
date1 = '2021年9月28日'
date2 = date1.replace("年", "/").replace("月", "/").replace("日", "")
print(date2)Method 2
Split the string into year, month, and day, then join with slashes.
# 方法二
# coding: utf-8
date1 = '2021年9月28日'
year = date1.split("年")[0]
month = date1.split("年")[1].split("月")[0]
day = date1.split("年")[1].split("月")[1].split("日")[0]
date2 = "/".join([year, month, day])
print(date2)Method 3
Use the datetime module to parse and reformat the date.
# 方法三
# coding: utf-8
import datetime
date1 = '2021年9月28日'
b = datetime.datetime.strptime('2021年9月28日', '%Y年%m月%d日')
date2 = b.strftime('%Y/%m/%d')
print(date2)Conclusion
These three methods demonstrate how to transform Chinese date strings into the standard YYYY/MM/DD format; you can choose the one that best fits your workflow and extend with additional techniques as needed.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
