from datetime import datetime
'''datetime时间模块'''
# 拿到当前计算机的系统时间 年-月-日 时:分:秒.毫秒
print(datetime.now())
print(type(datetime.now()))
2019-11-25 23:25:46.941824
<class 'datetime.datetime'>
from datetime import datetime
# 获取当前系统时间
now = datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S")
print(now)
print(type(now))
2019-11-27 21:40:30
<class 'str'>
from datetime import datetime
# 格林尼治时间,和我们相差8小时,utc协调世界时 Coordinated Universal Time
# coordinated 协调的
# universal 通用的
d1 = datetime.utcnow()
print(d1)
2019-11-22 13:27:33.084041
from datetime import datetime
# 用指定的时间创建datetime
print(datetime(2019, 11, 22, 12, 13, 14))
print(type(datetime(2019, 11, 22, 12, 13, 14)))
2019-11-22 12:13:14
<class 'datetime.datetime'>
from datetime import datetime
# 计算时间差
d1 = datetime(2019, 11, 22, 12, 13, 14)
d2 = datetime(2019, 11, 23, 14, 43, 14)
diff = d2 - d1
# seconds只计算当天的时分秒差(单位秒)
print(diff.seconds)
# total_seconds()计算年月日时分秒差(单位秒)
print(diff.total_seconds())
9000
95400.0
from datetime import datetime
# 计算时间差
d1 = datetime(2019, 11, 22, 12, 22 , 22)
d2 = datetime(2019, 11, 21, 12, 22, 22)
diff = d1 - d2
print(diff.seconds) # 单纯从时分秒来计算
print(diff.total_seconds()) # 包括年月日计算
0
86400.0
from datetime import datetime
def get_diff_datetime(d1, d2):
'''计算两个格式化时间之间差了多少年月日时分秒'''
diff = d2 - d1
year, days = divmod(diff.days, 365)
month, day = divmod(days, 30)
seconds = diff.seconds
hour, seconds2 = divmod(seconds, 3600)
minute, second = divmod(seconds2, 60)
return year, month, day, hour, minute, second
date1 = datetime(2018, 1, 2, 12, 30, 10)
date2 = datetime(2019, 6, 12, 18, 40, 20)
year, month, day, hour, minute, second = get_diff_datetime(date1, date2)
print(f"两个格式化时间之间差了{year}年{month}月{day}日{hour}时{minute}分{second}秒")
两个格式化时间之间差了1年5月11日6时10分10秒
from datetime import datetime
# 时间格式化strftime, format格式化
d = datetime(2019, 11, 22, 12, 13, 14)
print(d.strftime("%Y-%m-%d %H:%M:%S"))
print(type(d.strftime("%Y-%m-%d %H:%M:%S")))
print(d.strftime("%Y/%m/%d %H:%M:%S"))
print(type(d.strftime("%Y/%m/%d %H:%M:%S")))
2019-11-22 12:13:14
<class 'str'>
2019/11/22 12:13:14
<class 'str'>
from datetime import datetime
# 字符串转化为时间格式strptime, parse转化
d = "2019-11-22 12:13:14"
print(datetime.strptime(d, "%Y-%m-%d %H:%M:%S"))
print(type(datetime.strptime(d, "%Y-%m-%d %H:%M:%S")))
2019-11-22 12:13:14
<class 'datetime.datetime'>
from datetime import datetime
'''格式必须一致,不然会报错'''
# 字符串转化为日期时间格式
s = "2018-4-6 12:34:18"
print(datetime.strptime(s, "%Y/%m/%d %H:%M:%S"))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-d9390be238e2> in <module>()
5 # 字符串转化为日期时间格式
6 s = "2018-4-6 12:34:18"
----> 7 print(datetime.strptime(s, "%Y/%m/%d %H:%M:%S"))
~\Anaconda3\lib\_strptime.py in _strptime_datetime(cls, data_string, format)
563 """Return a class cls instance based on the input string and the
564 format string."""
--> 565 tt, fraction = _strptime(data_string, format)
566 tzname, gmtoff = tt[-2:]
567 args = tt[:6] + (fraction,)
~\Anaconda3\lib\_strptime.py in _strptime(data_string, format)
360 if not found:
361 raise ValueError("time data %r does not match format %r" %
--> 362 (data_string, format))
363 if len(data_string) != found.end():
364 raise ValueError("unconverted data remains: %s" %
ValueError: time data '2018-4-6 12:34:18' does not match format '%Y/%m/%d %H:%M:%S'