资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

python3日期函数 python3 日期

python之time模块

time 模块常用的与时间相关的类和函数:

目前创新互联已为1000+的企业提供了网站建设、域名、虚拟空间、网站托管、服务器托管、企业网站设计、巧家网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

time 模块的 struct_time 类代表一个时间对象,可以通过 索引和属性名 访问值。 对应关系如下:

索引 —— 属性 值

0 —— tm_year(年) 如:1945

1 —— tm_mon(月) 1 ~ 12

2 —— tm_mday(日) 1 ~ 31

3 —— tm_hour(时) 0 ~ 23

4 —— tm_min(分) 0 ~ 59

5 —— tm_sec(秒) 0 ~ 61

6 —— tm_wday(周) 0 ~ 6

7 —— tm_yday(一年内第几天) 1 ~ 366

8 —— tm_isdst(夏时令) -1、0、1

localtime() 表示当前时间,返回类型为 struct_time 对象 ,示例如下所示:

输出结果:

time()——返回当前时间的时间戳

gmtime([secs])——将时间戳转换为格林威治天文时间下的 struct_time,可选参数 secs 表示从 epoch 到现在的秒数,默认为当前时间

localtime([secs])——与 gmtime() 相似,返回当地时间下的 struct_time

mktime(t) localtime() 的反函数

asctime([t]) 接收一个 struct_time 表示的时间,返回形式为:Mon Dec 2 08:53:47 2019 的字符串

ctime([secs]) ctime(secs) 相当于 asctime(localtime(secs))

strftime(format[, t]) 格式化日期,接收一个 struct_time 表示的时间,并返回以可读字符串表示的当地时间

sleep(secs) 暂停执行调用线程指定的秒数

altzone 本地 DST 时区的偏移量,以 UTC 为单位的秒数

timezone 本地(非 DST)时区的偏移量,UTC 以西的秒数(西欧大部分地区为负,美国为正,英国为零)

tzname 两个字符串的元组:第一个是本地非 DST 时区的名称,第二个是本地 DST 时区的名称

基本使用如下所示:

strftime 函数日期格式化符号说明如下所示:

python,格式化时间实例,求

对于像'Wed, 11 Apr 2012 09:37:05 +0800'的时间格式化可如下解:

date='Wed, 11 Apr 2012 09:37:05 +0800'

dd=datetime.datetime.strptime(date,'%a, %d %b %Y %H:%M:%S %z')

dd.strftime('%Y-%m-%d %H:%M:%S')

Python格式化日期时间的函数为datetime.datetime.strftime();由字符串转为日期型的函数为:datetime.datetime.strptime(),两个函数都涉及日期时间的格式化字符串,列举如下:

%a Abbreviated weekday name   

%A Full weekday name   

%b Abbreviated month name   

%B Full month name   

%c Date and time representation appropriate for locale   

%d Day of month as decimal number (01 - 31)   

%H Hour in 24-hour format (00 - 23)   

%I Hour in 12-hour format (01 - 12)   

%j Day of year as decimal number (001 - 366)   

%m Month as decimal number (01 - 12)   

%M Minute as decimal number (00 - 59)   

%p Current locale's A.M./P.M. indicator for 12-hour clock   

%S Second as decimal number (00 - 59)   

%U Week of year as decimal number, with Sunday as first day of week (00 - 51)   

%w Weekday as decimal number (0 - 6; Sunday is 0)   

%W Week of year as decimal number, with Monday as first day of week (00 - 51)   

%x Date representation for current locale   

%X Time representation for current locale   

%y Year without century, as decimal number (00 - 99)   

%Y Year with century, as decimal number   

%z, %Z Time-zone name or abbreviation; no characters if time zone is unknown   

%% Percent sign

python里面有哪些自带函数?

python系统提供了下面常用的函数:

1. 数学库模块(math)提供了很多数学运算函数;

2.复数模块(cmath)提供了用于复数运算的函数;

3.随机数模块(random)提供了用来生成随机数的函数;

4.时间(time)和日历(calendar)模块提供了能处理日期和时间的函数。

注意:在调用系统函数之前,先要使用import 语句导入 相应的模块

该语句将模块中定义的函数代码复制到自己的程 序中,然后就可以访问模块中的任何函数,其方 法是在函数名前面加上“模块名.”。

希望能帮到你。

Python获取当前时间前、后一个月的函数

这需求折腾了我半天..

import time

import datetime as datetime

def late_time(time2):

# 先获得时间数组格式的日期

#time2是外部传入的任意日期

now_time = datetime.datetime.strptime(time2, '%Y-%m-%d')

#如需求是当前时间则去掉函数参数改写      为datetime.datetime.now()

threeDayAgo = (now_time - datetime.timedelta(days =30))

# 转换为时间戳

timeStamp =int(time.mktime(threeDayAgo.timetuple()))

# 转换为其他字符串格式

otherStyleTime = threeDayAgo.strftime("%Y-%m-%d")

return otherStyleTime

a = late_time("2019-3-30")

print(a)# 打印2018-02-28


当前文章:python3日期函数 python3 日期
文章链接:http://cdkjz.cn/article/dogssei.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220