获取当天日期
调用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秒’
#>>>
博客介绍了在Python中获取当天日期和当前时间的方法。通过调用locale函数和time函数来实现,还展示了不同格式的运行结果,如日期有‘2019年03月19日’‘2019-03-19’,时间有‘2019-03-19 07:09:07’等。
343

被折叠的 条评论
为什么被折叠?



