吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1035|回复: 17
收起左侧

[Python 原创] 生成中文格式的日期

[复制链接]
wapjsx 发表于 2023-12-22 16:43
在实际工作中,也许大家需要用到中文日期,不知道你们是怎么处理的???


自己动手 丰衣足食。


[Python] 纯文本查看 复制代码
import time


def myzhongwendate(arg_date='0000-00-00'): 
    if arg_date == '0000-00-00':
        mydate = time.strftime('%Y-%m-%d', time.localtime())   # 获取当前的日期,返回字符型数据,如:"2023-12-22"
    else:
        mydate = arg_date
    mydict_a = {'0': '〇', '1': '一', '2': '二', '3': '三', '4': "四",
                '5': '五', '6': '六', '7': '七', '8': '八', '9': '九', }
    # 定义一个字典 mydict_a ,用来处理年份
    _my_return_text = ''
    # 定义一个临时的字符型变量,用来存放生成的日期
    for i in mydate.split('-')[0]:
        _my_return_text += mydict_a[i]
    # 年份中各个数字进行处理
    _my_return_text += '年'
    # 处理年份结束。
    mydict_shiwei = {'0': '', '1': '十', '2': '二十', '3': '三十'}
    # 十位数的处理方式
    mydict_gewei = {'0': '', '1': '一', '2': '二', '3': '三', '4': "四",
                    '5': '五', '6': '六', '7': '七', '8': '八', '9': '九', }
    # 个位数的处理方式
    _my_return_text = _my_return_text + mydict_shiwei[mydate.split('-')[1][0]] + mydict_gewei[
        mydate.split('-')[1][1]] + "月"
    # 处理年月完成
    _my_return_text = _my_return_text + mydict_shiwei[mydate.split('-')[2][0]] + mydict_gewei[
        mydate.split('-')[2][1]] + "日"
    # 处理年月日完成
    return _my_return_text

if __name__ == "__main__":
    print(myzhongwendate('2001-01-28'))   # 指定时间生成 中文日期
    # 输出结果: 二〇〇一年一月二十八日
    print(myzhongwendate())               # 默认生成当天的中文日期
    # 输出结果: 二〇二三年十二月二十二日

免费评分

参与人数 3吾爱币 +6 热心值 +3 收起 理由
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
jz4128 + 1 + 1 我很赞同!
wkdxz + 1 我很赞同!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

qfxldhw 发表于 2023-12-22 18:23
本帖最后由 苏紫方璇 于 2023-12-24 22:13 编辑

[Python] 纯文本查看 复制代码
def arabic_to_chinese_number(number):
    """将阿拉伯数字转换为中文数字"""
    chinese_numerals = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"]
    return ''.join(chinese_numerals[int(digit)] for digit in str(number))

def chinese_date_format(year, month, day):
    """将年、月、日转换为完全的中文日期格式"""
    # 年份转换
    year_chinese = arabic_to_chinese_number(year) + "年"

    # 月份转换,10以上和10以下的月份处理方式略有不同
    if month < 10:
        month_chinese = arabic_to_chinese_number(month) + "月"
    else:
        month_chinese = "十" if month == 10 else "十" + arabic_to_chinese_number(month % 10) + "月"

    # 日的转换,分为1-9、10、11-19和20-31三种情况
    if day < 10:
        day_chinese = arabic_to_chinese_number(day) + "日"
    elif day == 10:
        day_chinese = "十日"
    elif day < 20:
        day_chinese = "十" + arabic_to_chinese_number(day % 10) + "日"
    else:
        day_chinese = arabic_to_chinese_number(day // 10) + "十" + arabic_to_chinese_number(day % 10) + "日" if day % 10 != 0 else arabic_to_chinese_number(day // 10) + "十日"

    return year_chinese + month_chinese + day_chinese

# 使用当前日期来测试函数
current_year = now.year
current_month = now.month
current_day = now.day
chinese_date = chinese_date_format(current_year, current_month, current_day)

chinese_date()

免费评分

参与人数 2吾爱币 +2 热心值 +2 收起 理由
wapjsx + 1 + 1 谢谢@Thanks!
苏紫方璇 + 1 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

EnterpriseSolu 发表于 2023-12-22 23:19
excel就有这个功能啊,我一开始也想用代码解决,后来想想,VBA的适用面更广泛,于是琢磨VBA的写法,OFFICE都可以用,这样更受欢迎
1045837055lucy 发表于 2023-12-22 17:06
ww886 发表于 2023-12-22 17:06
学习了,谢谢
kenxy 发表于 2023-12-22 17:16
我是直接手写的中文日期
Listentomusic 发表于 2023-12-22 17:27
学习了,
jz4128 发表于 2023-12-22 18:45
虽然用不上,但是学学也挺有意思
3969 发表于 2023-12-22 20:05
学习了,对我来说作用不是太大
li15103428 发表于 2023-12-22 20:10
学习了,谢谢
52soft 发表于 2023-12-22 20:11
qfxldhw 发表于 2023-12-22 18:23
def arabic_to_chinese_number(number):
    """将阿拉伯数字转换为中文数字"""
    chinese_numerals =  ...

报错NameError: name 'now' is not defined. Did you mean: 'pow'?
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-29 12:53

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表