简单的日期时间应用

日期时间应用

封装了一些时间日期,方便后边用到

#encoding=utf-8

import time

def get_current_year():  #获取当前时间年
    return str(time.localtime().tm_year) + '年'

def get_current_month():  #获取当前时间月
    if time.localtime().tm_mon < 10:
        month = '0'+str(time.localtime().tm_mon)
    else:
        month = str(time.localtime().tm_mon)
    return month + '月'

def get_current_day():  #获取当前时间日
    if time.localtime().tm_mday < 10:
        day = '0'+str(time.localtime().tm_mday)
    else:
        day = str(time.localtime().tm_mday)
    return day + '日'

def get_current_hour():  #获取当前时间时
    if time.localtime().tm_hour < 10:
        hour = '0'+str(time.localtime().tm_hour)
    else:
        hour = str(time.localtime().tm_hour)
    return hour + '时'

def get_current_date():  #获取当前时间年月日以反斜线分割
    return time.strftime('%Y/%m/%d', time.localtime())

def get_current_time(): #获取当前时间时分秒,不分割,截图专用
    return time.strftime('%H%M%S', time.localtime())

def get_english_current_date():  #获取当前时间年月日以中线分割
    return time.strftime('%Y-%m-%d', time.localtime())

def get_english_current_time():  #获取当前时间时分秒以冒号分割
    return time.strftime('%H:%M:%S',time.localtime())

def get_chinese_current_date():  #获取中文当前时间年月日
    year = str(time.localtime().tm_year)
    if time.localtime().tm_mon < 10:
        month = '0'+str(time.localtime().tm_mon)
    else:
        month = str(time.localtime().tm_mon)
    if time.localtime().tm_mday < 10:
        day = '0'+str(time.localtime().tm_mday)
    else:
        day = str(time.localtime().tm_mday)

    return '%s年%s月%s日' %(year, month, day)

def get_chinese_current_time():  #获取中文当前时间时分秒
    if time.localtime().tm_hour < 10:
        hour = '0'+str(time.localtime().tm_hour)
    else:
        hour = str(time.localtime().tm_hour)

    if time.localtime().tm_min < 10:
        minute = '0'+str(time.localtime().tm_min)
    else:
        minute = str(time.localtime().tm_min)
    if time.localtime().tm_sec < 10:
        sec = '0'+str(time.localtime().tm_sec)
    else:
        sec = str(time.localtime().tm_sec)

    return '%s时%s分%s秒' %(hour, minute, sec)

def get_chinese_current_datetime():  #获取中文当前时间年月日时分秒
    return get_chinese_current_date() + ' ' + get_chinese_current_time()

def get_english_current_datetime():  #获取当前时间年月日时分秒 中线冒号分割
    return get_english_current_date() + ' ' +get_english_current_time()

def get_chinese_anytime_date(str_date):  #获取中文任何时间年月日
    if not isinstance(str_date, int):
        return '时间戳输入错误'
    year = str(time.localtime(str_date).tm_year)
    if time.localtime(str_date).tm_mon < 10:
        month = '0'+str(time.localtime(str_date).tm_mon)
    else:
        month = str(time.localtime(str_date).tm_mon)
    if time.localtime(str_date).tm_mday < 10:
        day = '0'+str(time.localtime(str_date).tm_mday)
    else:
        day = str(time.localtime(str_date).tm_mday)

    return '%s年%s月%s日' %(year, month, day)

def get_chinese_anytime_time(str_time):  #获取中文任何时间时分秒
    if not isinstance(str_time,int):
        return '时间戳输入错误'
    if time.localtime(str_time).tm_hour < 10:
        hour = '0'+str(time.localtime(str_time).tm_hour)
    else:
        hour = str(time.localtime(str_time).tm_hour)

    if time.localtime(str_time).tm_min < 10:
        minute = '0'+str(time.localtime(str_time).tm_min)
    else:
        minute = str(time.localtime(str_time).tm_min)
    if time.localtime(str_time).tm_sec < 10:
        sec = '0'+str(time.localtime(str_time).tm_sec)
    else:
        sec = str(time.localtime(str_time).tm_sec)

    return '%s时%s分%s秒' %(hour, minute, sec)

def get_english_anytime_date(str_date):  #获取任何时间年月日以中线分割
    if not isinstance(str_date, int):
        return '时间戳输入错误'
    return time.strftime('%Y-%m-%d', time.localtime(str_date))

def get_english_anytime_time(str_time):  #获取任何时间时分秒 冒号分割
    return time.strftime('%H:%M:%S', time.localtime(str_time))



if __name__=='__main__':
    print('当前时间年= ', get_current_year())
    print('当前时间月= ', get_current_month())
    print('当前时间日= ', get_current_day())
    print('当前时间时= ', get_current_hour())
    print('当前时间年月日以/分开显示= ', get_current_date())
    print('当前时间时分秒以/分割= ', get_current_time())
    print('当前时间年月日以-分开显示= ', get_english_current_date())
    print('当前时间时分秒以:分开= ', get_english_current_time())
    print('中文当前时间年月日= ', get_chinese_current_date())
    print('中文当前时间时分秒= ', get_chinese_current_time())
    print('中文当前日期时间= ', get_chinese_current_datetime())
    print('当前日期时间以-:分割= ', get_english_current_datetime())
    print('中文某一时间年月日= ', get_chinese_anytime_date(1242424025))
    print('中文某一时间时分秒= ', get_chinese_anytime_time(43434))
    print('某一时间年月日以-分开显示= ', get_english_anytime_date(1242424575))
    print('某一时间时分秒以:分开= ', get_english_anytime_time(43434))




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值