前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python时间处理

Python时间处理

作者头像
Spaceack
发布2020-11-04 14:38:44
1.2K0
发布2020-11-04 14:38:44
举报
文章被收录于专栏:编程使我快乐

datetime 库

代码语言:javascript
复制
from datetime import datetime, timedelta
# 字符串 转 datetime类型
another_day_str = '2020-07-18 21:00:00'
another_day_datetime = datetime.strptime(another_day_str, '%Y-%m-%d %H:%M:%S')

# 时间加减运算, 增加1小时30分30秒
after_datetime = another_day_datetime + timedelta(hours=1, minutes=30, seconds=30)

# datetime类型 转 字符串 2020-07-18 22:30:30
after_str = after_datetime.strftime('%Y-%m-%d %H:%M:%S')

# 获取当前时间的字符串 格式化输出 20200719223030
now_datetime = datetime.now()
now_str = now_datetime.strftime('%Y%m%d%H%M%S')

# 转换为时间戳
timestamp = time.mktime(now_datetime.timetuple())

# 时间戳转换为datetime
datetime.datetime.fromtimestamp(timestamp)

# 获取间隔天数
day = (now_datetime - another_day_datetime).days

# 获取间隔秒数(不包含天数差)
second = (now_datetime - another_day_datetime).seconds

time库

代码语言:javascript
复制
import time
# 返回当前时间的时间戳(1970纪元后经过的浮点秒数)
timestamp = time.time()

# 时间戳转换为 struct_time类型(结构体时间)
struct_time = time.localtime(timestamp)

# 字符串转结构体时间 struct_time
now_str = '2020-07-19 21:00:00'
struct_time = time.strptime(now_str, "%Y-%m-%d %H:%M:%S")
# time.struct_time(tm_year=2020, tm_mon=7, tm_mday=19, tm_hour=21, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=201, tm_isdst=-1) <class 'time.struct_time'>

# struct_time 转 格式化字符串 20200719210000
str_time =  time.strftime('%Y%m%d%H%M%S', struct_time)

Commonly used format codes:

代码语言:javascript
复制
%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.

struct_time parameter

代码语言:javascript
复制
tm_gmtoff """offset from UTC in seconds"""
tm_hour   """hours, range [0, 23]"""
tm_isdst  """1 if summer time is in effect, 0 if not, and -1 if unknown"""
tm_mday  """day of month, range [1, 31]"""
tm_min   """minutes, range [0, 59]"""
tm_mon   """month of year, range [1, 12]"""
tm_sec   """seconds, range [0, 61])"""
tm_wday  """day of week, range [0, 6], Monday is 0"""
tm_yday  """day of year, range [1, 366]"""
tm_year  """year, for example, 1993"""
tm_zone  """abbreviation of timezone name"""
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-07-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • datetime 库
  • time库
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档