当前位置: 首页 > news >正文

济南企业网站制/搜索广告是什么意思

济南企业网站制,搜索广告是什么意思,搜狗网站做滤芯怎么样,建一个网站需要网站程序吗在平常的代码中,我们常常需要与时间打交道。在Python中,与时间处理有关的模块就包括:time,datetime,calendar(很少用,不讲),下面分别来介绍。 在开始之前,首先要说明几点: 一、在Pyt…


在平常的代码中,我们常常需要与时间打交道。在Python中,与时间处理有关的模块就包括:time,datetime,calendar(很少用,不讲),下面分别来介绍。

在开始之前,首先要说明几点:

一、在Python中,通常有这几种方式来表示时间:

  1. 时间戳
  2. 格式化的时间字符串
  3. 元组(struct_time)共九个元素。由于Python的time模块实现主要调用C库,所以各个平台可能有所不同。

二、几个定义

  • UTC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间,世界标准时间。在中国为UTC+8。DST(Daylight Saving Time)即夏令时。
  • 时间戳(timestamp)的方式:通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。
  • 元组(struct_time)方式:struct_time元组共有9个元素,返回struct_time的函数主要有gmtime(),localtime(),strptime()。下面列出这种方式元组中的几个元素:
索引(Index) 属性(Attribute)       值(Values)
0       tm_year(年)           比如2011 
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(weekday)        0 - 6(0表示周日)
7       tm_yday(一年中的第几天)     1 - 366
8       tm_isdst(是否是夏令时)     默认为-1

 

time模块的方法

time.time():返回当前时间的时间戳。
time.localtime([secs]):将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。
time.gmtime([secs]):和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。
time.mktime(t):将一个struct_time转化为时间戳。time.sleep(secs):线程推迟指定的时间运行。单位为秒。
time.asctime([t]):把一个表示时间的元组或者struct_time表示为这种形式:'Sun Oct 1 12:04:38 2017'。如果没有参数,将会将time.localtime()作为参数传入。
time.ctime([secs]):把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
* time.strftime(format[, t]):把一个代表时间的元组或者struct_time(如由time.localtime()和time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。
举例:time.strftime("%Y-%m-%d %X", time.localtime())#输出'2017-10-01 12:14:23'

*time.strptime(string[, format]):把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。举例:time.strptime('2017-10-3 17:54',"%Y-%m-%d %H:%M")#输出 time.struct_time(tm_year=2017, tm_mon=10, tm_mday=3, tm_hour=17, tm_min=54, tm_sec=0, tm_wday=1, tm_yday=276, tm_isdst=-1)

 

 

字符串转时间格式对应表

 1       Meaning                                                                       
 2 %a    Locale’s abbreviated weekday name.    
 3 %A    Locale’s full weekday name.    
 4 %b    Locale’s abbreviated month name.    
 5 %B    Locale’s full month name.    
 6 %c    Locale’s appropriate date and time representation.    
 7 %d    Day of the month as a decimal number [01,31].    
 8 %H    Hour (24-hour clock) as a decimal number [00,23].    
 9 %I    Hour (12-hour clock) as a decimal number [01,12].    
10 %j    Day of the year as a decimal number [001,366].    
11 %m    Month as a decimal number [01,12].    
12 %M    Minute as a decimal number [00,59].    
13 %p    Locale’s equivalent of either AM or PM.    (1)
14 %S    Second as a decimal number [00,61].    (2)
15 %U    Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.    (3)
16 %w    Weekday as a decimal number [0(Sunday),6].    
17 %W    Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.    (3)
18 %x    Locale’s appropriate date representation.    
19 %X    Locale’s appropriate time representation.    
20 %y    Year without century as a decimal number [00,99].    
21 %Y    Year with century as a decimal number.    
22 %z    Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].    
23 %Z    Time zone name (no characters if no time zone exists).    
24 %%    A literal '%' character.

最后为了容易记住转换关系,看下图

 

 

 

datetime模块


相比于time模块,datetime模块的接口则更直观、更容易调用

datetime模块定义了下面这几个类:

  • datetime.date:表示日期的类。常用的属性有year, month, day;
  • datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond;
  • datetime.datetime:表示日期时间。
  • datetime.timedelta:表示时间间隔,即两个时间点之间的长度。
  • datetime.tzinfo:与时区有关的相关信息。(这里不详细充分讨论该类,感兴趣的童鞋可以参考python手册)

我们需要记住的方法仅以下几个:

1.d=datetime.datetime.now() 返回当前的datetime日期类型d.timestamp(),d.today(), d.year,d.timetuple()等方法可以调用
2.datetime.date.fromtimestamp(322222) 把一个时间戳转为datetime日期类型3.时间运算>>> datetime.datetime.now() datetime.datetime(2017, 10, 1, 12, 53, 11, 821218)>>> datetime.datetime.now() + datetime.timedelta(4) #当前时间 +4天 datetime.datetime(2017, 10, 5, 12, 53, 35, 276589)>>> datetime.datetime.now() + datetime.timedelta(hours=4) #当前时间+4小时 datetime.datetime(2017, 10, 1, 16, 53, 42, 876275)
4.时间替换>>> d.replace(year=2999,month=11,day=30) datetime.date(2999, 11, 30)

 

转载于:https://www.cnblogs.com/wenyule/p/py-time_datetime.html

http://www.jmfq.cn/news/5272705.html

相关文章:

  • 有独立IP如何建设网站/百度推广关键词排名规则
  • 免费的网站生成app/网站网络营销公司
  • 个人网站怎么设计/重庆网站推广软件
  • 怎么做淘宝网站赚钱技巧/竞价排名采用什么计费方式
  • 网站的域名分为哪些/珠海百度seo
  • 湛江网站设计/灯塔网站seo
  • 网站用什么做备份/seo策略分析
  • 做亚马逊运营要看哪些网站/b站怎么推广
  • 设计网站的一般过程/产品推广活动策划方案
  • 江门网站开发/成免费的crm
  • 娱乐彩票网站建设制作/手机百度电脑版入口
  • 做企业网站设计价格是多少/网络营销比较常用的营销模式
  • wp网站打开太慢怎么做优化/合肥网络公司排名
  • 网站添加悬浮二维码/视频外链平台
  • 港湾有巢网站建设/热点事件
  • 金华电子商务网站建设/seo sem是什么意思
  • 企业网站 源代码/口红的推广软文
  • 手机如何做车载mp3下载网站/爱站长工具
  • 专门做餐饮ppt的网站/搜索引擎分类
  • 快速网站开发框架/百度怎么推广
  • wordpress 插件出错/站长之家seo查询官方网站
  • 多模室内设计网站/自动点击器
  • 网站建设能赚钱吗/合肥网络优化推广公司
  • 互联网与网站有哪些/搜索优化seo
  • 包装设计教程/上海网络优化服务
  • 昌吉网站建设哪家便宜/seo优化行业
  • 营销类网站建营销类网站建设/义乌最好的电商培训学校
  • 中英 网站模板 带手机版/营销软件网
  • 展示型网站开发/会员制营销方案
  • 微信公众号 做不了微网站吗/环球资源网官方网站