from datetime import datetime
now = datetime.now()
current_time = now.strftime("%H:%M:%S") # H - hour, M- minute, S - second
print("Current Time =", current_time)
获取当前时间
from datetime import datetime
now = datetime.now()
current_hour = now.strftime("%H")
print("Current hour =", current_hour)
获取当前日期
from datetime import date
today = date.today()
print("Today's date:", today)
同样地,用%S表示秒,用%M表示分钟,用%H表示小时。
和{}表示日,{}表示月,以及{}表示年
额外费用
同时打印日期和时间
from datetime import datetime
# datetime object containing current date and time
now = datetime.now()
print("now =", now)
# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)
An idealized time, independent of any particular day, assuming that every day has exactly 246060 seconds. (There is no notion of “leap seconds” here.) Attributes: hour, minute, second, microsecond, and tzinfo.
在Python中使用
datetime
模块用Python 3给出的示例
获取当前时间
获取当前时间
获取当前日期
同样地,用}表示日,{}表示月,以及{}表示年
%S
表示秒,用%M
表示分钟,用%H
表示小时。 和{额外费用
同时打印日期和时间
按时区打印
资料来源:
Date
Time
也请查看:Similar question
你很接近一个答案。我们开始吧
导入datetime模块后,只需调用:
如果您想访问数据,可以使用
year
、month
、day
、hour
、minute
、second
、microsecond
方法:要指定给定的小时数,只需使用datetime.time类的变量
和以前一样。访问数据可以通过调用其方法来完成
这是您的文档:) datetime module
相关问题 更多 >
编程相关推荐