捋一捋操作python日期时间处理(上)

简介: 正式的Python专栏第32篇,同学站住,别错过这个从0开始的文章!

讲了很多数据容器操作,这篇我们看看时间的处理。

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

  • 获取当前时间
  • 获取系统秒数(从纪元时间开始)
  • 日期跟秒数之间转换
  • 获取日历等
  • 日期格式化显示输出

这些都非常常见

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

import time
import calender

我们看看这两个模块。

time 内置模块

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/10 22:49 下午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : __init__.py.py
# @Project : hello
import time
# 从19700101 零时刻开始计算经过多少秒,精确到微秒
ticks = time.time()
print("ticks=", ticks)
#获取当前时间
print(time.localtime())

运行效果如下:

image.png

这个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)

运行效果如下:

image.png

calender 内置模块

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/10 22:49 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : calendardemo.py
# @Project : hello
import calendar
cal = calendar.month(2021, 11)
print("cal:", cal)

至今输出一个月份,这个在Java的Calendar中也没有。太直接了。


image.png

日期格式化处理

这里我们使用了time模块的strftime(str from time):

#第一个参数为格式,第二个参数为时间
time.strftime("%Y-%m-%d %H:%M:%S %Z", gmtime))
#!/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", 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", localtime))
print(time.strftime("%Y-%m-%d %H:%M:%S %Z", localtime))  # 打印日期加上时区
# 试试其他格式
print(time.strftime("%D", localtime))
print(time.strftime("%T", localtime))

稍微解释一下:

%Y-%m-%d %H:%M:%S %Z 对应的是

年份4位数-月份-日期 小时:分钟:秒数 时区信息

%b 则是三个字母英文输出月份,比如Jan/Feb 等。

下面是运行结果:

image.png

总结

Python 提供的日期处理都非常简单,但是在创建日期方面使用time模块没有那么方便,需要对应元组下标才行。

目录
相关文章
|
4月前
|
Python
Datetime模块应用:Python计算上周周几对应的日期
Datetime模块应用:Python计算上周周几对应的日期
96 1
|
4月前
|
Python
Python编程获取当前日期的所属周日期信息
Python编程获取当前日期的所属周日期信息
77 1
|
3月前
|
Python
在 Python 中,如何将日期时间类型转换为字符串?
在 Python 中,如何将日期时间类型转换为字符串?
148 64
|
3月前
|
Python
在 Python 中,如何将字符串中的日期格式转换为日期时间类型?
在 Python 中,如何将字符串中的日期格式转换为日期时间类型?
61 6
|
3月前
|
数据挖掘 Python
用Python轻松获取任意月份的公休日期
本文介绍了如何使用Python的`calendar`和`datetime`模块轻松获取任意月份的公休日期,包括周六和周日。通过示例代码,用户可以输入年份和月份,程序将输出该月份的所有公休日。这对于安排会议、规划旅行或数据分析都非常有用。
43 3
|
4月前
|
数据挖掘 iOS开发 MacOS
利用Python计算农历日期
利用Python计算农历日期
200 4
|
4月前
|
数据处理 Python
Python编程-利用datetime模块生成当前年份之前指定的间隔所有年份的日期列表和csv文件
Python编程-利用datetime模块生成当前年份之前指定的间隔所有年份的日期列表和csv文件
33 1
|
5月前
|
数据挖掘 索引 Python
# Python 判断入参日期是周几
# Python 判断入参日期是周几 原创
43 2
|
4月前
|
调度 开发者 Python
python超详细的日期操作【建议收藏备用】
python超详细的日期操作【建议收藏备用】
43 0
|
4月前
|
Python
使用python计算两个日期之前的相差天数,周数
使用python计算两个日期之前的相差天数,周数
65 0

热门文章

最新文章