Python基础学习之时间转换函数用法详解

 更新时间:2019年06月18日 09:56:20   作者:George-Henry  
这篇文章主要介绍了Python基础学习之时间转换函数用法,结合实例形式分析了Python常见的日期时间获取、转换相关操作技巧,需要的朋友可以参考下

本文实例讲述了Python基础学习之时间转换函数用法。分享给大家供大家参考,具体如下:

前言

python的时间格式分为多种,几种格式之间的转换方法时常是我们遇到的而且是经常忘记的点,python不像php,时间字符串和datetime是一起的,只需要strtotime和date函数就可以相互转化。虽然网上已经有很多python时间转换的文章,但是由于作者本人经常做海外业务,需要各种时区之间的转换,所以这篇文章会对按时区转换各种时间格式做一个总结。

转换方法图示(图片转自网络):

一、字符串转时间戳

1、默认:

import time
def time_str_to_timestamp(string_time, _format="%Y-%m-%d %H:%M:%S"):
  return int(time.mktime(time.strptime(string_time, _format)))

2、按时区转:

import time
import datetime
from pytz import timezone as tz
def time_str_to_timestamp_by_timezone(string_time, _format="%Y-%m-%d %H:%M:%S”, from_tz=“UTC”, to_tz="America/Los_Angeles"):
  from_tz = tz(from_tz)
  to_tz = tz(to_tz)
  return int(time.mktime(
    datetime.datetime.strptime(string_time, _format).replace(
      tzinfo=from_tz).astimezone(to_tz).timetuple()))

二、时间戳转字符串

1、默认:

import time
def timestamp_to_str(timestamp, _format="%Y-%m-%d %H:%M:%S"):
  return time.strftime(_format, time.localtime(timestamp))

2、按时区转:

import datetime
from pytz import timezone as tz
def timestamp_to_str_by_timezone(timestamp, _format="%Y-%m-%d %H:%M:%S”, to_tz="America/Los_Angeles"):
  to_tz = tz(to_tz)
  return str(datetime.datetime.fromtimestamp(timestamp, to_tz).strftime(_format))

三、字符串转datetime

1、默认:

import datetime
def datetime_str_to_datetime(string_time, _format="%Y-%m-%d %H:%M:%S"):
  return datetime.datetime.strptime(string_time, _format)

2、按时区转:

import datetime
from pytz import timezone as tz
def datetime_str_to_datetime_by_timezone(string_time, from_tz=“UTC”, to_tz="America/Los_Angeles”, _format="%Y-%m-%d %H:%M:%S",):
  from_tz = tz(from_tz)
  to_tz = tz(to_tz)
  return datetime.datetime.strptime(string_time, _format).replace(
        tzinfo=from_tz).astimezone(to_tz)

四、datetime转字符串

1、默认:

import datetime
def datetime_to_datetime_str(date, _format="%Y-%m-%d %H:%M:%S"):
  return date.strftime(_format)

2、按时区转:

import datetime
from pytz import timezone as tz
def datetime_to_datetime_str_by_timezone(date, from_tz=“UTC”, to_tz="America/Los_Angeles”, _format="%Y-%m-%d %H:%M:%S"):
  from_tz = tz(from_tz)
  to_tz = tz(to_tz)
  date = date.replace(tzinfo=from_tz).astimezone(to_tz)
  return date.strftime(_format)

五、datetime转时间戳

1、默认:

import time
def datetime_to_timestamp(date):
  return int(time.mktime(date.timetuple()))

2、按时区转:

import time
from pytz import timezone as tz
def datetime_to_timestamp_by_timezone(date, from_tz=“UTC”, to_tz="America/Los_Angeles"):
  from_tz = tz(from_tz)
  to_tz = tz(to_tz)
  return int(time.mktime(date.replace(
      tzinfo=from_tz).astimezone(to_tz).timetuple()))

六、时间戳转datetime

1、默认:

import datetime
def timestamp_to_datetime(time_stamp):
  return datetime.datetime.fromtimestamp(time_stamp)

2、按时区转:

import datetime
from pytz import timezone as tz
def timestamp_to_datetime_by_timezone(time_stamp, to_tz="America/Los_Angeles"):
  to_tz = tz(to_tz)
  return datetime.datetime.fromtimestamp(time_stamp, to_tz)

PS:这里再为大家推荐几款关于日期与天数计算的在线工具供大家使用:

在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi

在线万年历日历:
http://tools.jb51.net/bianmin/wannianli

在线阴历/阳历转换工具:
http://tools.jb51.net/bianmin/yinli2yangli

Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python日期与时间操作技巧总结》、《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

  • 详解Python编程中time模块的使用

    详解Python编程中time模块的使用

    这篇文章主要介绍了详解Python编程中time模块的使用,是Python入门学习中的基础知识,需要的朋友可以参考下
    2015-11-11
  • Python Web App开发Dockerfiles编写示例

    Python Web App开发Dockerfiles编写示例

    这篇文章主要为大家介绍了Python Web App编写Dockerfiles的示例代码,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • Python登录系统界面实现详解

    Python登录系统界面实现详解

    这篇文章主要介绍了Python登录系统界面实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,,需要的朋友可以参考下
    2019-06-06
  • Python with的用法

    Python with的用法

    with 语句是从 Python 2.5 开始引入的一种与异常处理相关的功能。with 语句适用于对资源进行访问的场合,确保不管使用过程中是否发生异常都会执行必要的“清理”操作,释放资源,比如文件使用后自动关闭、线程中锁的自动获取和释放等。
    2014-08-08
  • 解决Python字典查找报Keyerror的问题

    解决Python字典查找报Keyerror的问题

    这篇文章主要介绍了解决Python字典查找报Keyerror的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-05-05
  • Python face_recognition实现AI识别图片中的人物

    Python face_recognition实现AI识别图片中的人物

    最近碰到了照片识别的场景,正好使用了face_recognition项目,给大家分享分享。face_recognition项目能做的很多,人脸检测功能也是有的,是一个比较成熟的项目。感兴趣的可以了解一下
    2022-01-01
  • ubuntu 18.04搭建python环境(pycharm+anaconda)

    ubuntu 18.04搭建python环境(pycharm+anaconda)

    这篇文章主要为大家详细介绍了ubuntu 18.04搭建python环境,包括Anaconda安装、Pycharm安装及初始配置,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06
  • PyTorch实现卷积神经网络的搭建详解

    PyTorch实现卷积神经网络的搭建详解

    这篇文章主要为大家介绍了PyTorch实现卷积神经网络的搭建详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • Python实现在一行中交换两个变量

    Python实现在一行中交换两个变量

    这篇文章主要为大家详细介绍了如何利用Python语言实现在一行中交换两个变量功能,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-08-08
  • pandas与pyspark计算效率对比分析

    pandas与pyspark计算效率对比分析

    这篇文章主要介绍了pandas与pyspark计算效率对比,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-06-06

最新评论