日期时间应用
封装了一些时间日期,方便后边用到
#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))