# -*- coding: UTF-8 -*-
# 时间操作
from datetime import datetime
# datetime.now方法返回当前时间,返回值类型是datetime类型
datetime_time = datetime.now()
print(datetime_time)
# 2024-06-20 19:18:50.519291
# datetime.strftime方法将datetime对象转为指定格式的字符串
datetime_time = datetime.now()
string_time = datetime_time.strftime("%Y-%m-%d %H:%M:%S")
print(string_time)
# 2024-06-20 19:18:50
# datetime.strptime方法将字符串转为datetime对象,转换时要求字符串内容符合指定的格式
string_time = "2024-06-20 19:20:25"
datetime_time = datetime.strptime(string_time, "%Y-%m-%d %H:%M:%S")
print(datetime_time)
# 2024-06-20 19:20:25
from datetime import date
# date类:主要用于处理年、月、日;
d = date(2024, 6, 21)
print(d)
# 2024-06-21
from datetime import time
# time类:主要用于处理时、分、秒;
t = time(12, 00, 00)
print(t)
# 12:00:00
from datetime import datetime
# datetime类:date类和time类的综合使用,可以处理年、月、日、时、分、秒;
dt = datetime(2024, 6, 20, 12, 00, 00)
print(dt)
# 2024-06-20 12:00:00
'''
参考:
https://geek-docs.com/python/python-ask-answer/15_python_how_to_convert_a_string_date_into_datetime_format_in_python.html
'''