您的位置:首页 > 编程语言 > Python开发

Python--获取当前日期和时间(含中文格式)

2019-03-22 21:59 531 查看

获取当天日期
调用locale函数
调用time函数

def get_current_date(is_chinese=False):
import time
import locale
if not is_chinese :
return time.strftime('%Y-%m-%d')
elif is_chinese:
locale.setlocale(locale.LC_CTYPE, 'chinese')
return time.strftime('%Y年%m月%d日')

#运行结果
#>>> get_current_date(True)
#‘2019年03月19日’
#>>> get_current_date()
#‘2019-03-19’

获取当前时间

def get_current_time(is_chinese=False):
import time
import locale
if not is_chinese :
return time.strftime('%Y-%m-%d %H:%M:%S')
elif is_chinese:
locale.setlocale(locale.LC_CTYPE, 'chinese')
return time.strftime('%Y年%m月%d日%H时%M分%S秒')

#运行结果
次设定为群#>>> get_current_time()
#‘2019-03-19 07:09:07’
#>>> get_current_time(True)
#‘2019年03月19日07时09分13秒’
#>>>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: