Python datetime 获得时间戳 年月日时分秒

214 阅读1分钟
  •        Python : 3.8.13
  •          OS : Windows 21H1
  •       Conda : 4.12.0
  •     PyCharm : 2022.1 (Community Edition)

代码

from datetime import datetime

# 在创建文件夹时可以用到(时间戳 - 年月日)
# 在创建文件时可以用到(时间戳 - 年月日时分秒)

# 时间戳 - 年月日
print("{}".format(datetime.now().strftime('%Y-%m-%d')))

# 时间戳 - 年月日时分秒
print("{}".format(datetime.now().strftime('%Y%m%d-%H%M%S')))

运行

D:\Develop\Anaconda3\envs\pyqtenv\python.exe C:/Users/admin/Desktop/pythonProject/main.py
2022-05-09
20220509-225520

Process finished with exit code 0

学习推荐


Python具有开源、跨平台、解释型和交互式等特性,值得学习。
Python的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。
代码的书写要遵守规范,这样有助于沟通和理解。
每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。