基本数据获取:
In [38]: import datetime as dt In [39]: on = dt.datetime.now() #获取当前准确时间 In [40]: on Out[40]: datetime.datetime(2018, 9, 17, 15, 5, 7, 30934) In [41]: print(on) 2018-09-17 15:05:07.030934 In [42]: to = dt.datetime.today() #获取今天的时间,today与now功能类似,理论上说now更加精确 In [43]: to Out[43]: datetime.datetime(2018, 9, 17, 15, 5, 42, 279086) In [44]: print(to) 2018-09-17 15:05:42.279086 In [45]: print(type(to)) <class 'datetime.datetime'> In [46]: dt.datetime.today().weekday() #拿到datetime对象,可以直接取里面的属性, Out[46]: 0 #这个月的第一周 In [47]: d = dt.datetime(2018,7,30,16,10,50) #直接构造一个datetime对象 In [48]: d Out[48]: datetime.datetime(2018, 7, 30, 16, 10, 50) In [49]: print(d) 2018-07-30 16:10:50 In [50]: print(d.year) #取各种属性信息 2018 In [51]: print(d.month) 7 In [52]: print(d.day) 30 In [53]: print(d.hour) 16
对于datetime这种对象来说,还可以将日期信息转化为序数表示,这样就可以直接比较大小了
In [54]: o = d.toordinal() In [55]: o Out[55]: 736905
但是如果想要将序数转化为具体的时间,那么可能会丢失一些信息。
In [56]: dt.datetime.fromordinal(o) Out[56]: datetime.datetime(2018, 7, 30, 0, 0) In [57]: t = dt.datetime.time(d) In [58]: t Out[58]: datetime.time(16, 10, 50) In [59]: type(t) Out[59]: datetime.time
datetime这个模块还能提取date信息:
In [60]: dd = dt.datetime.date(d) In [61]: print(dd) 2018-07-30 In [62]: print(type(dd)) <class 'datetime.date'> In [63]: d Out[63]: datetime.datetime(2018, 7, 30, 16, 10, 50) In [64]: d.replace(second=0, microsecond=0) Out[64]: datetime.datetime(2018, 7, 30, 16, 10)
同样可以获取time信息:
In [65]: t = dt.datetime.now() In [66]: t Out[66]: datetime.datetime(2018, 9, 17, 15, 10, 12, 122225) In [67]: t.time() #获取时间信息 Out[67]: datetime.time(15, 10, 12, 122225) #在time对象中,还可以求一个最大值或者最小值 In [68]: d4_start = datetime.datetime.combine(datetime.date.today(), datetime.time.min) In [69]: d4_stop = datetime.datetime.combine(datetime.date.today(),datetime.time.max) In [70]: print(d4_start,d4_stop) 2018-09-17 00:00:00 2018-09-17 23:59:59.999999
timedelta:timedelta是另一类对象,在日期的数学运算中会经常使用。
In [71]: td = dt.datetime.now() -d In [72]: td Out[72]: datetime.timedelta(48, 82845, 699889)
#我们可以直接获得 In [73]: print(td.days) 48 In [74]: print(td.seconds) #两个时间之间差距的小时数 82845 In [75]: print(td.microseconds) 699889 In [76]: print(td.total_seconds()) 4230045.699889
利用这种时差,我们可以预算出之后的时间
In [77]: d3 = dt.date.today() + dt.timedelta(days=1) In [78]: print(d3) 2018-09-18
datetime对象与其他数据类型之间的转换
In [79]: d
Out[79]: datetime.datetime(2018, 7, 30, 16, 10, 50)
转换成字符串:
In [80]: sd =d.isoformat() In [81]: print(type(sd),sd) <class 'str'> 2018-07-30T16:10:50
也可以转化成你想要的任何格式:
In [82]: std = d.strftime("%A, %d. %B %Y %I:%M%p") In [83]: print(type(std),std) <class 'str'> Monday, 30. July 2018 04:10PM In [84]: dt.datetime.strptime('2017-03-31', '%Y-%m-%d') Out[84]: datetime.datetime(2017, 3, 31, 0, 0) In [85]: dt.datetime.strptime('30-4-16','%d-%m-%y') Out[85]: datetime.datetime(2016, 4, 30, 0, 0)
当然也可以使用python内置的函数直接进行转换
In [86]: ds = str(d) In [87]: ds Out[87]: '2018-07-30 16:10:50'
utcnow:
该函数给出了UTC形式的日期和时间的准确信息。
通俗来说就是世界标准时间,过去称作格林威治标准时间,简称GMT。
In [88]: dt.datetime.now() Out[88]: datetime.datetime(2018, 9, 17, 15, 15, 48, 249462) In [89]: dt.datetime.utcnow() Out[89]: datetime.datetime(2018, 9, 17, 7, 15, 56, 246227) In [90]: shicha = dt.datetime.now() - dt.datetime.utcnow() In [91]: shicha Out[91]: datetime.timedelta(0, 28800) In [92]: shicha.seconds Out[92]: 28800 In [93]: shicha.seconds/60/60 Out[93]: 8.0
datetime与time之间时间格式转换:
datetime转化为string:
In [95]: d5 = datetime.datetime.now() In [96]: print(type(d5),d5) <class 'datetime.datetime'> 2018-09-17 15:17:23.449068 In [97]: d5_s = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") In [98]: print(type(d5_s),d5_s) <class 'str'> 2018-09-17 15:17:55
string转化为datetime:
In [99]: d6 = datetime.datetime.strptime('2018-4-10 10:20:30',"%Y-%m-%d %H:%M:%S") In [100]: print(d6,type(d6)) 2018-04-10 10:20:30 <class 'datetime.datetime'>
timetuple转化为datetime:
In [101]: d7 = datetime.datetime.now().timetuple() In [102]: print(d7,type(d7)) time.struct_time(tm_year=2018, tm_mon=9, tm_mday=17, tm_hour=15, tm_min=18, tm_sec=59, tm_wday=0, tm_yday=260, tm_isdst=-1) <class 'time.struct_time'>
datetime转化为date:
In [103]: d8 = datetime.datetime.now().date() In [104]: print(d8,type(d8)) 2018-09-17 <class 'datetime.date'>
datetime转化为timestamp(时间戳):
In [105]: import time In [106]: now = datetime.datetime.now() In [107]: timestamp = time.mktime(now.timetuple()) In [108]: print(timestamp) 1537168799.0
timestamp转化为datetime:
In [109]: d9 = datetime.datetime.fromtimestamp(time.time()) In [110]: print(d9,type(d9)) 2018-09-17 15:20:24.252870 <class 'datetime.datetime'>