python获取当天日期进行格式转换

# Python Library
import time

def getToday(format=3):
    """返回今天的日期字串"""
    # format=1      20190305
    # format=2      08:26
    # format=3      2019/03/05
    # format=4      2019/03/05 08:26
    # format=5      190305
    # format=6      2019-03-05
    # format=7      2019/03/05 08:25:40
    # format=8      2019-03-05 08:25
    # format=9      2019-03-05 08:24:51
    # format=10     2019年03月05日 08时50分55秒
    t = time.time()
    date_ary = time.localtime(t)
    print(date_ary)
    if format == 1:
        x = time.strftime("%Y%m%d", date_ary)
    elif format == 2:
        x = time.strftime("%H:%M", date_ary)
    elif format == 3:
        x = time.strftime("%Y/%m/%d", date_ary)
    elif format == 4:
        x = time.strftime("%Y/%m/%d %H:%M", date_ary)
    elif format == 5:
        x = time.strftime("%y%m%d", date_ary)
    elif format == 6:
        x = time.strftime("%Y-%m-%d", date_ary)
    elif format == 7:
        x = time.strftime("%Y/%m/%d %H:%M:%S", date_ary)
    elif format == 8:
        x = time.strftime("%Y-%m-%d %H:%M", date_ary)
    elif format == 9:
        x = time.strftime("%Y-%m-%d %H:%M:%S", date_ary)
    elif format == 10:
        # 将时间元组转换为自定义的字符串格式
        try:
            x = time.strftime("%Y年%m月%d日  %H:%M", date_ary)
        except UnicodeEncodeError as e:
            # 打印错误
            print(e)
        finally:
            x = time.strftime('%Y{y}%m{m}%d{d} %H{h}%M{f}%S{s}').format(y='年', m='月', d='日', h='时', f='分', s='秒')
            print(x)
    return x

print(getToday(format=10))

Original: https://www.cnblogs.com/apollo1616/p/10474590.html
Author: 阿波罗Apollo
Title: python获取当天日期进行格式转换

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/10664/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部
最近整理资源【免费获取】:   👉 程序员最新必读书单  | 👏 互联网各方向面试题下载 | ✌️计算机核心资源汇总