python标准库之_time datatime

常用的python标准库有:

1)os(操作系统相关。如:创建、打开、编辑一个文件)

2)time datatime(时间与日期相关)

3)科学计算相关 (math)

4)网络请求相关(urline)

 

time模块主要是用来获取当前时间以及时间格式的模块。常用的方法:

time.asctime()    国外的时间格式

time.time()   时间戳。以纪元(1970.1.1.0.0.0开始),返回当前距离纪元时间的秒数。任意时刻输出的时间戳都是唯一的

time.sleep()  等待。参数为s

time.localtime()   时间戳转成时间元组

time.strftime()  将当前时间戳转换成带格式的时间

 

练习:获取两天前的时间,并转换成带格式的时间输出。代码示例如下:

 

 

 time.strftime()格式定义如下:

%Y  Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
选择需要的内容输出即可。如代码示例中,只输出了(年月日 时分秒)。对应方法具体使用查看操作:在pychar中按住ctrl键+鼠标点击对应方法,即可查看到该方法的使用介绍。或者上python官网也能查看到

 

posted @ 2021-03-10 22:41  Brynaaa_111  阅读(78)  评论(0)    收藏  举报