日期格式转换:从 年-月-日 到 月/日/年

本文介绍如何将日期从 '年-月-日' 格式转换为 '月/日/年' 格式,并确保个位数日期前添加 '0'。

以下是一些示例日期及其转换结果:

原始日期 (年-月-日) | 转换后的日期 (月/日/年) ---|--- 1949-1-23 | 01/23/1949 1959-4-3 | 04/03/1959 11-7-1962 | 11/07/1962 ... | ... 1978-9-15 | 09/15/1978 5-20-1964 | 05/20/1964

您可以使用各种编程语言或文本编辑器工具来执行此日期格式转换。

例如,在 Python 中,您可以使用以下代码:

def format_date(date_str):
  year, month, day = date_str.split('-')
  return f'{int(month):02}/{int(day):02}/{year}'

date_str = '1949-1-23'
formatted_date = format_date(date_str)
print(formatted_date)  # 输出:01/23/1949

这段代码将输入日期字符串拆分为年、月、日,然后使用 f-string 格式化输出,确保月和日均为两位数,并在个位数前添加 '0'。

希望这篇文章能帮助您理解如何进行日期格式转换!

日期格式转换:将 年-月-日 转换为 月/日/年

原文地址: https://www.cveoy.top/t/topic/jPnU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录