import datetime
dt = datetime.datetime.now()
print(1,dt)
print(2,dt.date(),dt.time())
print(3,dt.year,dt.month,dt.day,dt.isoweekday())
print('-----------------------------')
ts = dt.timestamp()
print(4,ts)
print(5,datetime.datetime.fromtimestamp(ts))
print('-----------------------------')
dt2 = dt.replace(2015,11,11)
print(6,dt2)
print('-----------------------------')
dt3 = datetime.datetime(2020,11,11,11,11,11)
print(7,dt3)
print('-----------------------------')
dt4 = datetime.datetime.strptime("2020-12-12 12:12:12","%Y-%m-%d %H:%M:%S")
print(8,dt4)
print('-----------------------------')
dt5 = print(9,dt.strftime("%Y^%m^%d %H:%M:%S"))
print('-----------------------------')
print(10,"{} {} {}".format(dt4.year,dt4.month,dt4.day))
print(11,"{0:%H} {0:%M} {0:%S}".format(dt))
print('-----------------------------')
old = datetime.datetime.now()
print(12,old)
t_delta = datetime.timedelta(hours=24)
new = old + t_delta
print(13,new)
print(14,t_delta.total_seconds())
运行结果:
1 2019-05-26 08:06:19.493747
2 2019-05-26 08:06:19.493747
3 2019 5 26 7
-----------------------------
4 1558829179.493747
5 2019-05-26 08:06:19.493747
-----------------------------
6 2015-11-11 08:06:19.493747
-----------------------------
7 2020-11-11 11:11:11
-----------------------------
8 2020-12-12 12:12:12
-----------------------------
9 2019^05^26 08:06:19
-----------------------------
10 2020 12 12
11 08 06 19
-----------------------------
12 2019-05-26 08:06:19.498494
13 2019-05-27 08:06:19.498494
14 86400.0