详解python日期时间处理2

作者:雷学委 时间:2021-05-20 19:27:15 

前篇我们稍微学习了Python中时间的获取,这次继续学习日期的时区转换,格式化等等。

开发中常用的日期操作还有哪些?

  • 时区转换显示

  • 日期格式化

  • 秒数 与 日期 与 字符串的转换

我们经常会用到,比如全球化的业务根据不同客户显示不同时间(格式等)

在python 主要有下面两个模块涵盖了常用日期处理


import time
import calender

我们看看这两个模块。

时间处理中的类型转换:struct_time vs str

Python中创建一个时间,具体来说创建一个struct_time 需要一个9个元素的元组来构造。

asctime 函数帮我们把这种类型的时间格式化为字符串。


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/10 22:49 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : createtime.py
# @Project : hello

import time
# fixed time: time.struct_time(tm_year=2021, tm_mon=11, tm_mday=10, tm_hour=22, tm_min=55, tm_sec=11, tm_wday=16, tm_yday=16, tm_isdst=16)
the9fields = (2021, 11, 10, 22, 55, 11, 16, 16, 16)
fixed = time.struct_time(the9fields)
print("fixed time:", fixed)
print("type:", type(fixed))
result = time.asctime(the9fields)  # 类似struct_time,需要9个元素构成的元组参数。
print("asc time:", result)
print("type:", type(result))
localtime = time.localtime()
print("local time:", localtime)
print("type:", type(localtime))
print("asc time:", time.asctime(localtime))

运行效果如下:

详解python日期时间处理2

这个ticks就是从0时刻计算,至今的秒数累计。

可以隔一秒运行这个程序,每次ticks值加上1(近似)

指定输入来构造时间:


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/10 22:49 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : createtime.py
# @Project : hello

import time
#fixed time: time.struct_time(tm_year=2021, tm_mon=11, tm_mday=10, tm_hour=22, tm_min=55, tm_sec=11, tm_wday=16, tm_yday=16, tm_isdst=16)
fixed = time.struct_time((2021, 11, 10, 22, 55, 11, 16, 16, 16))
print("fixed time:", fixed)

运行效果如下:

详解python日期时间处理2

时间与字符串转换


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/10 22:49 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : createtime2.py
# @Project : hello

import time
sec = 3600  # * 开始后的一个小时(GMT 19700101凌晨)
#
gmtime = time.gmtime(sec)
print("gmtime:", gmtime)  # GMT
print("type:", type(gmtime))
print(time.strftime("%b %d %Y %H:%M:%S", gmtime))
print(time.strftime("%Y-%m-%d %H:%M:%S %Z", gmtime))  # 打印日期加上时区
print("*" * 16)
localtime = time.localtime(sec)
print("localtime:", localtime)  # 本地时间
print("type:", type(localtime))
print(time.strftime("%b %d %Y %H:%M:%S", localtime))
print(time.strftime("%Y-%m-%d %H:%M:%S %Z", localtime))  # 打印日期加上时区
#试试其他格式
print(time.strftime("%D", localtime))
print(time.strftime("%T", localtime))

下面是运行结果:

详解python日期时间处理2

对于时间格式化函数(strftime) 它并不理会你传入的时间(struct_time)是哪个时区的,照样给你输出,也是正确的。

但是我们写程序拿数据的时候,必须把时区信息原样返回到用户端,或者是UI端,最后由客户端本地时区设置进行调整显示。

最后看看,日期文本转换为日期(struct_time).


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/10 7:49 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : createtime4.py
# @Project : hello
import time
print("strptime1:", time.strptime("Jan 01 1970 09:00:00", "%b %d %Y %H:%M:%S"))
print("strptime2:", time.strptime("1970-01-01 09:00:00", "%Y-%m-%d %H:%M:%S"))
print("strptime3:", time.strptime("1970-01-01 09:00:00 CST", "%Y-%m-%d %H:%M:%S %Z"))

下面是运行结果:

详解python日期时间处理2

来源:https://levin.blog.csdn.net/article/details/121551375

标签:python,日期,时间,处理
0
投稿

猜你喜欢

  • 使用pycharm和pylint检查python代码规范操作

    2023-06-06 08:02:38
  • Python如何安装第三方模块

    2023-08-01 12:50:07
  • 网页常用特效整理:中级篇

    2013-07-15 13:43:32
  • 一篇文章彻底搞懂Python中可迭代(Iterable)、迭代器(Iterator)与生成器(Generator)的概念

    2023-11-03 23:52:38
  • 使用python怎样产生10个不同的随机数

    2021-08-12 13:07:18
  • PHP session反序列化漏洞深入探究

    2023-05-30 04:53:04
  • python lambda表达式(匿名函数)写法解析

    2023-07-30 20:56:12
  • 对python3.4 字符串转16进制的实例详解

    2022-03-29 16:15:17
  • 如何将自己的python库打包成wheel文件并上传到pypi

    2022-03-20 06:13:40
  • 在Python 2.7即将停止支持时,我们为你带来了一份python 3.x迁移指南

    2022-03-04 21:11:24
  • Python 中对 XML 文件的编码转换问题

    2022-08-19 12:46:53
  • asp,php,.net使用301重定向方法

    2007-09-26 14:05:00
  • 基于Go语言实现分金币游戏

    2023-06-22 06:49:28
  • 菜鸟课堂:MSSQL的安全设置问题解答

    2009-10-29 13:22:00
  • python实现多线程暴力破解登陆路由器功能代码分享

    2023-08-28 21:27:01
  • python pyqtgraph 保存图片到本地的实例

    2023-05-08 01:15:11
  • Django实现图片文字同时提交的方法

    2021-10-19 20:11:28
  • python使用opencv驱动摄像头的方法

    2023-08-26 17:00:49
  • 使用Javascript实现选择下拉菜单互移并排序

    2023-09-07 18:14:45
  • 使用go求幂的几种方法小结

    2023-09-23 05:07:45
  • asp之家 网络编程 m.aspxhome.com