前言:
最近一直在更新Python基础教程,小伙伴们反映也不错,咱们今天继续更新。
在Python开发中经常会与时间打交道,如:获取事件戳,时间戳的格式化等,今天咱们简要记录一下python操作时间的方法。
python中常见的处理时间的模块:
time:处理时间的模块,如获取时间戳,格式化日期等
datetime:date和time的结合体,处理日期和时间
calendar:日历相关的模块,如:处理年历/月历
time模块介绍
1.时间戳 --> 时间元组格式(time.struct_time) --> 日期字符串
2.日期字符串 --> 时间元组格式(time.struct_time) --> 时间戳
3.获取当前时间的分钟/秒
4.获取整分钟/整小时时间戳
datetime模块介绍
datetime模块中常见的类:
datetime.date:处理日期
datetime.time:处理时间
datetime.datetime:处理日期和时间
datetime.timedelta:处理时间差
time模块
1.时间转字符串

2.字符串转时间

3.unix时间戳转字符串

4.字符串转unix时间戳

5.unix时间戳转时间

6.时间转unix时间戳

属于datetime的方法和属性
1、 date(…):返回datetime对象的日期部分:

2、time(…):返回datetime对象的时间部分:

3、utctimetuple(…):返回UTC时间元组:

4、combine(…):将一个date对象和一个time对象合并生成一个datetime对象:

5、now(…):返回当前日期时间的datetime对象:

6、utcnow(…):返回当前日期时间的UTC datetime对象:

7、strptime(…):根据string, format 2个参数,返回一个对应的datetime对象:

8、utcfromtimestamp(…):UTC时间戳的datetime对象,时间戳值为time.time():

附录:python中时间日期格式化符号:
calendar说明:
这里介绍一下使用month(year, month)方法打印出某年某月下的月历时间
例子:

喜欢小编请多多点赞评论转发,你们的支持就是小编最大的动力,帮小编来一波关注呦~~~
另外小编也帮大家整理了一些Python模块的学习资料,有需要的请关注小编,并加微信 bjmsb1 来免费领取吧~~~

网友评论