python获取年月日时分秒毫秒

 

from datetime import datetime

# 获取当前时间
now = datetime.now()

# 获取年、月、日、时、分、秒和毫秒
year = now.year
month = now.month
day = now.day
hour = now.hour
minute = now.minute
second = now.second
millisecond = now.microsecond // 1000  # 毫秒需要除以1000,因为microsecond返回的是微秒

print(f"年: {year}")
print(f"月: {month}")
print(f"日: {day}")
print(f"时: {hour}")
print(f"分: {minute}")
print(f"秒: {second}")
print(f"毫秒: {millisecond}")

 

 

 

######################

posted @ 2023-09-29 20:17  西北逍遥  阅读(901)  评论(0编辑  收藏  举报