string转time_常用Time模块的功能Python

7b5724adbe48a92c7fe713ef82026808.png

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

  1. 时间戳(timestamp), 表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。例子:1554864776.161901
  1. 格式化的时间字符串,比如“2020-10-03 17:54”
  1. 元组(struct_time)共九个元素。由于Python的time模块实现主要调用C库,所以各个平台可能有所不同,mac上:time.struct_time(tm_year=2020, tm_mon=4, tm_mday=10, tm_hour=2, tm_min=53, tm_sec=15, tm_wday=2, tm_yday=100, tm_isdst=0)
    1. 索引(Index) 属性(Attribute) 值(Values)0 tm_year(年) 比如20111 tm_mon(月) 1 - 122 tm_mday(日) 1 - 313 tm_hour(时) 0 - 234 tm_min(分) 0 - 595 tm_sec(秒) 0 - 616 tm_wday(weekday) 0 - 6(0表示周日)7 tm_yday(一年中的第几天) 1 - 3668 tm_isdst(是否是夏令时) 默认为-1

UTC时间

UTC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间,世界标准时间。在中国为UTC+8,又称东8区。DST(Daylight Saving Time)即夏令时。

time模块的方法

  • time.localtime([secs]):将一个时间戳转换为当前时区的struct_time。若secs参数未提供,则以当前时间为准。
  • time.gmtime([secs]):和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。
  • time.time():返回当前时间的时间戳。
  • time.mktime(t):将一个struct_time转化为时间戳。
  • time.sleep(secs):线程推迟指定的时间运行,单位为秒。
  • time.asctime([t]):把一个表示时间的元组或者struct_time表示为这种形式:’Sun Oct 1 12:04:38 2019’。如果没有参数,将会将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)
  • 这里整理了一个字符串转时间格式对应表

c7542d29104ef60532b44615dcbcdb38.png
Python Time模块转换
  • MeaningNotes%aLocale’s abbreviated weekday name.%ALocale’s full weekday name.%bLocale’s abbreviated month name.%BLocale’s full month name.%cLocale’s appropriate date and time representation.%dDay of the month as a decimal number [01,31].%HHour (24-hour clock) as a decimal number [00,23].%IHour (12-hour clock) as a decimal number [01,12].%jDay of the year as a decimal number [001,366].%mMonth as a decimal number [01,12].%MMinute as a decimal number [00,59].%pLocale’s equivalent of either AM or PM.(1)%SSecond as a decimal number [00,61].(2)%UWeek 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)%wWeekday as a decimal number [0(Sunday),6].%WWeek 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)%xLocale’s appropriate date representation.%XLocale’s appropriate time representation.%yYear without century as a decimal number [00,99].%YYear with century as a decimal number.%zTime 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].%ZTime zone name (no characters if no time zone exists).%%A literal ‘%’character.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值