hopeless-dream

导航

Python模块-----日期模块

datetime模块

datetime类的类方法 返回的都是datetime类实例化后的对象

today()返回本地失去的当前时间的datetime对象

In [90]: import datetime

In [91]: datetime.datetime.today()
Out[91]: datetime.datetime(2020, 9, 25, 17, 29, 12, 94402)

now(tz=None) 放回当前时间datetime对象,时间到微妙,如果tz为None,返回和today()相同

In [3]: datetime.datetime.now()
Out[3]: datetime.datetime(2020, 9, 25, 18, 5, 59, 826495)

当使用时区tz的时候会报错

In [6]: datetime.datetime.now(8)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-dd94d7ca2e60> in <module>
----> 1 datetime.datetime.now(8)

TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'int'

使用第三方模块pytz解决

In [2]: pip install pytz
Requirement already satisfied: pytz in c:\users\asus-pc\appdata\local\programs\python\python35\lib\site-packages
You are using pip version 9.0.1, however version 20.2.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
Note: you may need to restart the kernel to use updated packages.

In [4]: import pytz

In [13]: pytz.timezone(pytz.country_timezones('us')[1])
Out[13]: <DstTzInfo 'America/Detroit' LMT-1 day, 18:28:00 STD>

In [14]: us_tz=pytz.timezone(pytz.country_timezones('us')[1])

In [15]: datetime.datetime.now(us_tz)
Out[15]: datetime.datetime(2020, 9, 25, 6, 15, 15, 825404, tzinfo=<DstTzInfo 'America/Detroit' EDT-1 day, 20:00:00 DST>)

In [16]: datetime.datetime.now()
Out[16]: datetime.datetime(2020, 9, 25, 18, 15, 22, 882819)

utcnow() 没有时区的当前时间,比当前时区的慢8个小时

In [17]: datetime.datetime.utcnow()
Out[17]: datetime.datetime(2020, 9, 25, 10, 17, 21, 792524)

fromtimestamp(timestamp,tz=None)

In [19]: datetime.datetime.fromtimestamp(1601000625.553865)
Out[19]: datetime.datetime(2020, 9, 25, 10, 23, 45, 553865)

datetime对象的方法

构造datetime对象

In [20]: datetime.datetime(2010,1,3,10,29,25)
Out[20]: datetime.datetime(2010, 1, 3, 10, 29, 25)

datetime对象的属性(year、month、day、hour、minute、second、microsecond)

In [22]: tm=datetime.datetime(2010,1,3,10,29,25)

In [23]: tm.year
Out[23]: 2010

In [25]: tm.minute
Out[25]: 29

weekday() 返回周的某天,周一为0

In [27]: tm=datetime.datetime(1988,11,26,10,29,25)

In [28]: tm.weekday()
Out[28]: 5

isoweekday() 返回周的某天

In [29]: tm.isoweekday()
Out[29]: 6

date() 返回日期date对象

In [31]: tm.date()
Out[31]: datetime.date(1988, 11, 26)

time() 返回时间time对象

In [32]: tm.time()
Out[32]: datetime.time(10, 29, 25)

replace() 修改并返回新的时间

In [33]: tm.replace(11)
Out[33]: datetime.datetime(11, 11, 26, 10, 29, 25)

In [34]: tm.replace(1999,7,1)
Out[34]: datetime.datetime(1999, 7, 1, 10, 29, 25)

isocalendar() 返回一个三元组(年,第几周,该周的某一天)

In [35]: tm.isocalendar()
Out[35]: (1988, 47, 6)

timestamp()返回到微秒的时间戳(从时间元年到现在)

In [18]: datetime.datetime.utcnow().timestamp()
Out[18]: 1601000625.553865

Python日期格式化

类方法strptime(date_string,format) 返回datetime对象

In [38]: time_str=datetime.datetime.strptime('2019/10/11 18:53',"%Y/%m/%d %H:%M")

In [39]: time_str
Out[39]: datetime.datetime(2019, 10, 11, 18, 53)

对象方法 strftime(format) 返回字符串

In [41]: time_str.strftime('%H:%M:%S %d-%m-%y')
Out[41]: '18:53:00 11-10-19'
In [45]: "{0:%H}:{0:%M}:{0:%S} {0:%d}-{0:%m}-{0:%y}".format(time_str)
Out[45]: '18:53:00 11-10-19'

timedelta对象

构造方法

datetime.timedelta(days=0,seconds=0,microseconds=0,milliseconds=0,minutes=0,hours=0,weeks=0)
year=datetime.timedelta(days=360)

计算

In [46]: datetime.timedelta(hours=8)
Out[46]: datetime.timedelta(0, 28800)

In [47]: t_d=datetime.timedelta(hours=8)

In [48]: datetime.datetime.now()
Out[48]: datetime.datetime(2020, 9, 25, 19, 35, 57, 915668)

In [49]: datetime.datetime.now()-t_d
Out[49]: datetime.datetime(2020, 9, 25, 11, 36, 5, 375506)

求时间戳

In [52]: datetime.timedelta(hours=8).total_seconds()
Out[52]: 28800.0

time标准库

sleep(seconds) 挂起当前进程多少秒

In [55]: time.sleep(2)

 localtime() 返回当前时间的struct_time格式

语法:

localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)

In [3]: lt=time.localtime()

In [4]: lt
Out[4]: time.struct_time(tm_year=2021, tm_mon=7, tm_mday=9, tm_hour=3, tm_min=13, tm_sec=57, tm_wday=4, tm_yday=190, tm_isdst=1)

  In [17]: type(time.localtime(2))
  Out[17]: time.struct_time

gmtime() 返回UTC时间的struct_time格式

语法:

gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst)

In [5]: lt2=time.gmtime()

In [6]: lt2
Out[6]: time.struct_time(tm_year=2021, tm_mon=7, tm_mday=9, tm_hour=7, tm_min=15, tm_sec=59, tm_wday=4, tm_yday=190, tm_isdst=0)

  In [8]: lt2.tm_hour
  Out[8]: 7

mktime(tuple) 返回时间元年到指定时间的时间戳

语法:

mktime(tuple) -> floating point number

In [24]: datetime.datetime.utcnow().timestamp(),time.mktime(time.localtime())
Out[24]: (1625830138.296473, 1625815738.0)

In [25]: datetime.datetime.utcnow().timestamp(),time.mktime(time.localtime(2))
Out[25]: (1625830363.588682, 2.0)

strftime()  返回时间的字符串格式

语法:

strftime(format[, tuple]) -> string

In [26]: time.strftime("%Y-%m-%d",time.localtime())
Out[26]: '2021-07-09'

strptime(string, format) 转换字符串格式时间为结构化时间

语法:

strptime(string, format) -> struct_time

In [30]: time.strptime("2020-12-10","%Y-%m-%d")
Out[30]: time.struct_time(tm_year=2020, tm_mon=12, tm_mday=10, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=345, tm_isdst=-1)

 

posted on 2020-09-25 18:48  hopeless-dream  阅读(309)  评论(0编辑  收藏  举报