Python 时间日期处理库函数
标准库
datetime
>>> import datetime
>>> date = datetime.date(2023, 12, 20)
>>> print(date)
2023-12-20
>>> date = datetime.datetime(2023, 12, 20)
>>> print(date)
2023-12-20 00:00:00
>>> print(date.strftime('%Y-%m-%d'))
2023-12-20
>>> today = datetime.date.today()
>>> print(today)
2023-12-20
>>> today.strftime('%A')
'Wednesday'
>>> today.weekday()
2 # 0代表星期天,1代表星期一,以此类推
>>> today.isoweekday()
3 # 1代表星期一,以此类推,7代表星期天
>>> datetime.date(2023, 12, 24).weekday()
6
>>> datetime.date(2023, 12, 24).isoweekday()
7
dateutil
>>> from dateutil import parser
>>> date = '2023-12-20'
>>> parser.parse(date)
datetime.datetime(2023, 12, 20, 0, 0)
>>> parser.parse(date).strftime('%Y-%m-%d')
'2023-12-20'
>>> parser.parse(date).strftime('%A')
'Wednesday'
>>> parser.parse(date).weekday()
2
calendar
>>> import calendar
>>> calendar.weekday(2023, 12, 20)
2
附: 日期时间常用的格式
%Y:四位数的年份(例如2023)%y:两位数的年份(例如23)%m:两位数的月份(01到12)%d:两位数的日期(01到31)%H:24小时制的小时数(00到23)%I:12小时制的小时数(01到12)%M:两位数的分钟数(00到59)%S:两位数的秒数(00到59)%f:微秒数(000000到999999)%p:AM或PM%A:完整的星期名称(例如Monday星期一)%a:简写的星期名称(例如Mon星期一)%B:完整的月份名称(例如January一月)%b:简写的月份名称(例如Jan一月)%c:日期和时间,使用系统默认的格式%x:日期,使用系统默认的格式%X:时间,使用系统默认的格式%Z:时区名称或缩写
第三方库
arrow
安装: pip install arrow
>>> import arrow
>>> arrow.Arrow(2023, 12, 20)
<Arrow [2023-12-20T00:00:00+00:00]>
>>> arrow.Arrow(2023, 12, 20, 20, 30)
<Arrow [2023-12-20T20:30:00+00:00]>
>>> arrow.utcnow()
<Arrow [2023-12-20T13:03:30.505669+00:00]>
>>> arrow.utcnow().ctime()
'Wed Dec 20 13:03:48 2023'
>>> arrow.utcnow().date()
datetime.date(2023, 12, 20)
>>> arw = arrow.utcnow()
>>> arw.dehumanize("2 days ago")
<Arrow [2023-12-18T13:04:54.443942+00:00]>
>>> import arrow
>>> arrow.Arrow(2023, 12, 20)
<Arrow [2023-12-20T00:00:00+00:00]>
>>> arrow.Arrow(2023, 12, 20, 20, 30)
<Arrow [2023-12-20T20:30:00+00:00]>
>>> arw = arrow.utcnow()
>>> arw
<Arrow [2023-12-20T13:05:46.025519+00:00]>
>>> arw.dehumanize("2 days ago")
<Arrow [2023-12-18T13:05:46.025519+00:00]>
>>> arw.dehumanize("in a month")
<Arrow [2024-01-20T13:05:46.025519+00:00]>
>>> arw.format('YYYY-MM-DD')
'2023-12-20'
>>> arw.format('YYYY-MM-DD HH:mm:ss ZZ')
'2023-12-20 13:05:46 +00:00'
>>> arw.format()
'2023-12-20 13:05:46+00:00'
>>> arw.format('YYYY-MM-DD HH:mm')
'2023-12-20 13:05'
>>> arw.isocalendar()
datetime.IsoCalendarDate(year=2023, week=51, weekday=3)
>>> arw.isoformat()
'2023-12-20T13:05:46.025519+00:00'
>>> arw.isoweekday()
3
>>> arw.weekday()
2
>>> arw.replace(year=2024, month=6)
<Arrow [2024-06-20T13:05:46.025519+00:00]>
>>> arw.replace(year=2024, month=6).format('YYYY-MM-DD')
'2024-06-20'
>>> arw.shift(days=4)
<Arrow [2023-12-24T13:05:46.025519+00:00]>
>>> arw.shift(days=-2)
<Arrow [2023-12-18T13:05:46.025519+00:00]>
>>> arw.time()
datetime.time(13, 5, 46, 25519)
>>> arw.timetuple()
time.struct_time(tm_year=2023, tm_mon=12, tm_mday=20, tm_hour=13, tm_min=5, tm_sec=46, tm_wday=2, tm_yday=354, tm_isdst=0)
时间段分割 .range()
>>> import arrow
>>> start = arrow.Arrow(2023, 12, 10, 12, 30)
>>> end = arrow.Arrow(2023, 12, 20, 6, 30)
>>> for r in arrow.Arrow.range('day', start, end):
print(r)
2023-12-10T12:30:00+00:00
2023-12-11T12:30:00+00:00
2023-12-12T12:30:00+00:00
2023-12-13T12:30:00+00:00
2023-12-14T12:30:00+00:00
2023-12-15T12:30:00+00:00
2023-12-16T12:30:00+00:00
2023-12-17T12:30:00+00:00
2023-12-18T12:30:00+00:00
2023-12-19T12:30:00+00:00
>>> end = arrow.Arrow(2023, 12, 12, 6, 30)
>>> for r in arrow.Arrow.range('hour', start, end):
print(r)
2023-12-10T12:30:00+00:00
2023-12-10T13:30:00+00:00
2023-12-10T14:30:00+00:00
2023-12-10T15:30:00+00:00
2023-12-10T16:30:00+00:00
2023-12-10T17:30:00+00:00
2023-12-10T18:30:00+00:00
2023-12-10T19:30:00+00:00
2023-12-10T20:30:00+00:00
2023-12-10T21:30:00+00:00
2023-12-10T22:30:00+00:00
2023-12-10T23:30:00+00:00
2023-12-11T00:30:00+00:00
2023-12-11T01:30:00+00:00
2023-12-11T02:30:00+00:00
2023-12-11T03:30:00+00:00
2023-12-11T04:30:00+00:00
2023-12-11T05:30:00+00:00
2023-12-11T06:30:00+00:00
2023-12-11T07:30:00+00:00
2023-12-11T08:30:00+00:00
2023-12-11T09:30:00+00:00
2023-12-11T10:30:00+00:00
2023-12-11T11:30:00+00:00
2023-12-11T12:30:00+00:00
2023-12-11T13:30:00+00:00
2023-12-11T14:30:00+00:00
2023-12-11T15:30:00+00:00
2023-12-11T16:30:00+00:00
2023-12-11T17:30:00+00:00
2023-12-11T18:30:00+00:00
2023-12-11T19:30:00+00:00
2023-12-11T20:30:00+00:00
2023-12-11T21:30:00+00:00
2023-12-11T22:30:00+00:00
2023-12-11T23:30:00+00:00
2023-12-12T00:30:00+00:00
2023-12-12T01:30:00+00:00
2023-12-12T02:30:00+00:00
2023-12-12T03:30:00+00:00
2023-12-12T04:30:00+00:00
2023-12-12T05:30:00+00:00
2023-12-12T06:30:00+00:00
pendulum
还有一个时间处理库 ,内容更加丰富有空再研究研究。
NAME
pendulum
PACKAGE CONTENTS
__version__
_helpers
_pendulum
constants
date
datetime
day
duration
exceptions
formatting (package)
helpers
interval
locales (package)
mixins (package)
parser
parsing (package)
testing (package)
time
tz (package)
utils (package)
CLASSES
builtins.object
pendulum.formatting.formatter.Formatter
datetime.date(builtins.object)
pendulum.date.Date(pendulum.mixins.default.FormattableMixin, datetime.date)
pendulum.datetime.DateTime(datetime.datetime, pendulum.date.Date)
datetime.datetime(datetime.date)
pendulum.datetime.DateTime(datetime.datetime, pendulum.date.Date)
datetime.time(builtins.object)
pendulum.time.Time(pendulum.mixins.default.FormattableMixin, datetime.time)
datetime.timedelta(builtins.object)
pendulum.duration.Duration
pendulum.interval.Interval
datetime.tzinfo(builtins.object)
pendulum.tz.timezone.FixedTimezone(datetime.tzinfo, pendulum.tz.timezone.PendulumTimezone)
enum.IntEnum(builtins.int, enum.ReprEnum)
pendulum.day.WeekDay
pendulum.mixins.default.FormattableMixin(builtins.object)
pendulum.date.Date(pendulum.mixins.default.FormattableMixin, datetime.date)
pendulum.datetime.DateTime(datetime.datetime, pendulum.date.Date)
pendulum.time.Time(pendulum.mixins.default.FormattableMixin, datetime.time)
pendulum.tz.timezone.PendulumTimezone(abc.ABC)
pendulum.tz.timezone.FixedTimezone(datetime.tzinfo, pendulum.tz.timezone.PendulumTimezone)
pendulum.tz.timezone.Timezone(zoneinfo.ZoneInfo, pendulum.tz.timezone.PendulumTimezone)
zoneinfo.ZoneInfo(datetime.tzinfo)
pendulum.tz.timezone.Timezone(zoneinfo.ZoneInfo, pendulum.tz.timezone.PendulumTimezone)
相关文章:
Python 时间日期处理库函数
标准库 datetime >>> import datetime >>> date datetime.date(2023, 12, 20) >>> print(date) 2023-12-20 >>> date datetime.datetime(2023, 12, 20) >>> print(date) 2023-12-20 00:00:00 >>> print(date.strfti…...
第二十二章 : Spring Boot 集成定时任务(一)
第二十二章 : Spring Boot 集成定时任务(一) 前言 本章知识点: 介绍使用Spring Boot内置的Scheduled注解来实现定时任务-单线程和多线程;以及介绍Quartz定时任务调度框架:简单定时调度器(Simp…...
关于“Python”的核心知识点整理大全32
目录 12.6.4 调整飞船的速度 settings.py ship.py alien_invasion.py 12.6.5 限制飞船的活动范围 ship.py 12.6.6 重构 check_events() game_functions.py 12.7 简单回顾 12.7.1 alien_invasion.py 12.7.2 settings.py 12.7.3 game_functions.py 12.7.4 ship.py …...
【krita】实时绘画 入门到精通 海报+电商+装修+人物
安装插件 首先打开comfyUI,再打开krita,出现问题提示, 打开 cd custom_nodes 输入命令 安装控件 git clone https://github.com/Acly/comfyui-tooling-nodes.git krita基础设置 设置模型 设置lora (可设置lora强度 增加更多…...
云原生系列2-CICD持续集成部署-GitLab和Jenkins
1、CICD持续集成部署 传统软件开发流程: 1、项目经理分配模块开发任务给开发人员(项目经理-开发) 2、每个模块单独开发完毕(开发),单元测试(测试) 3、开发完毕后,集成部…...
50ms时延工业相机
华睿工业相机A3504CG000 参数配置: 相机端到端理论时延:80ms 厂家同步信息,此款设备帧率上线23fps,单帧时延:43.48ms,按照一图缓存加上传输显示的话,厂家预估时延在:80ms 厂家还有…...
CPU缓存一致性问题
什么是可见性问题? Further Reading :什么是可见性问题? 缓存一致性 内存一致性 内存可见性 顺序一致性区别 CPU缓存一致性问题 由于CPU缓存的出现,很好地解决了处理器与内存速度之间的矛盾,极大地提高了CPU的吞吐能…...
35道HTML高频题整理(附答案背诵版)
1、简述 HTML5 新特性 ? HTML5 是 HTML 的最新版本,它引入了很多新的特性和元素,以提供更丰富的网页内容和更好的用户体验。以下是一些主要的新特性: 语义元素:HTML5 引入了新的语义元素,像 <article&g…...
【powershell】Windows环境powershell 运维之历史文件压缩清理
🦄 个人主页——🎐开着拖拉机回家_Linux,大数据运维-CSDN博客 🎐✨🍁 🪁🍁🪁🍁🪁🍁🪁🍁 🪁🍁🪁&am…...
【Linux】Linux线程概念和线程控制
文章目录 一、Linux线程概念1.什么是线程2.线程的优缺点3.线程异常4.线程用途5.Linux进程VS线程 二、线程控制1.线程创建2.线程终止3.线程等待4.线程分离 一、Linux线程概念 1.什么是线程 线程是进程内的一个执行流。 我们知道,一个进程会有对应的PCB,…...
Flink cdc3.0同步实例(动态变更表结构、分库分表同步)
文章目录 前言准备flink环境docker构建mysql、doris环境数据准备 通过 FlinkCDC cli 提交任务整库同步同步变更路由变更路由表结构不一致无法同步 结尾 前言 最近Flink CDC 3.0发布, 不仅提供基础的数据同步能力。schema 变更自动同步、整库同步、分库分表等增强功…...
国产Apple Find My认证芯片哪里找,伦茨科技ST17H6x芯片可以帮到您
深圳市伦茨科技有限公司(以下简称“伦茨科技”)发布ST17H6x Soc平台。成为继Nordic之后全球第二家取得Apple Find My「查找」认证的芯片厂家,该平台提供可通过Apple Find My认证的Apple查找(Find My)功能集成解决方案。…...
肺癌相关知识
写在前面 大概想了解下肺癌相关的知识,开此贴做记录,看看后续有没有相关的生信文章思路。 综述 文章名期刊影响因子Lung cancer immunotherapy: progress, pitfalls, and promisesMol Cancer37.3 常见治疗手段有surgery, radiation therapy, chemoth…...
ChimeraX使用教程-安装及基本操作
ChimeraX使用教程-安装及基本操作 1、访问https://www.cgl.ucsf.edu/chimerax/download.html进行下载,然后安装 安装完成后,显示界面 2、基本操作 1、点击file,导入 .PDB 文件。 (注:在 alphafold在线预测蛋白》点…...
【小黑嵌入式系统第十一课】μC/OS-III程序设计基础(一)——任务设计、任务管理(创建基本状态内部任务)、任务调度、系统函数
上一课: 【小黑嵌入式系统第十课】μC/OS-III概况——实时操作系统的特点、基本概念(内核&任务&中断)、与硬件的关系&实现 文章目录 一、任务设计1.1 任务概述1.2 任务的类型1.2.1 单次执行类任务(运行至完成型&#…...
Redis一些常用的技术
文章目录 第1关:Redis 事务与锁机制第2关:流水线第3关:发布订阅第4关:超时命令第5关:使用Lua语言 第1关:Redis 事务与锁机制 编程要求 根据提示,在右侧编辑器Begin-End补充代码,根据…...
基于QPainter 绘图图片绕绘制设备中心旋转
项目地址:https://gitcode.com/m0_45463480/QPainter/tree/main 获取途径:进入CSDN->GitCode直接下载或者通过git拉取仓库内容。 QPainter是Qt框架中的一个类,用于在QWidget或QPixmap等设备上进行绘图操作。它提供了丰富的绘图功能,可以用于绘制线条、图形、文本等。Q…...
计算机网络(4):网络层
网络层提供的两种服务 虚电路服务(Virtual Circuit Service)和数据报服务(Datagram Service)是在网络层(第三层)提供的两种不同的通信服务。它们主要区别在于建立连接的方式和数据传输的方式。 虚电路服务…...
动态内存分配(malloc和free、calloc和realloc)
目录 一、为什么要有动态内存分配 二、C/C中程序内存区域划分 三、malloc和free 2.1、malloc 2.2、free 四、calloc和realloc 3.1、calloc 3.2、realloc 3.3realloc在调整内存空间的是存在两种情况: 3.4realloc有malloc的功能 五、常见的动…...
C语言---井字棋(三子棋)
Tic-Tac-Toe 1 游戏介绍和随机数1.1 游戏介绍1.2 随机数的生成1.3 棋盘大小和符号 2 设计游戏2.1 初始化棋盘2.2 打印棋盘2.3 玩家下棋2.4 电脑下棋2.5 判断输赢2.6 game()函数2.7 main()函数 3 完整三子棋代码3.1 Tic_Tac_Toe.h3.2 Tic_Tac_Toe.c3.3 Test.c 4 游戏代码的缺陷 …...
C++_核心编程_多态案例二-制作饮品
#include <iostream> #include <string> using namespace std;/*制作饮品的大致流程为:煮水 - 冲泡 - 倒入杯中 - 加入辅料 利用多态技术实现本案例,提供抽象制作饮品基类,提供子类制作咖啡和茶叶*//*基类*/ class AbstractDr…...
调用支付宝接口响应40004 SYSTEM_ERROR问题排查
在对接支付宝API的时候,遇到了一些问题,记录一下排查过程。 Body:{"datadigital_fincloud_generalsaas_face_certify_initialize_response":{"msg":"Business Failed","code":"40004","sub_msg…...
(二)TensorRT-LLM | 模型导出(v0.20.0rc3)
0. 概述 上一节 对安装和使用有个基本介绍。根据这个 issue 的描述,后续 TensorRT-LLM 团队可能更专注于更新和维护 pytorch backend。但 tensorrt backend 作为先前一直开发的工作,其中包含了大量可以学习的地方。本文主要看看它导出模型的部分&#x…...
大数据零基础学习day1之环境准备和大数据初步理解
学习大数据会使用到多台Linux服务器。 一、环境准备 1、VMware 基于VMware构建Linux虚拟机 是大数据从业者或者IT从业者的必备技能之一也是成本低廉的方案 所以VMware虚拟机方案是必须要学习的。 (1)设置网关 打开VMware虚拟机,点击编辑…...
P3 QT项目----记事本(3.8)
3.8 记事本项目总结 项目源码 1.main.cpp #include "widget.h" #include <QApplication> int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); } 2.widget.cpp #include "widget.h" #include &q…...
css的定位(position)详解:相对定位 绝对定位 固定定位
在 CSS 中,元素的定位通过 position 属性控制,共有 5 种定位模式:static(静态定位)、relative(相对定位)、absolute(绝对定位)、fixed(固定定位)和…...
SpringTask-03.入门案例
一.入门案例 启动类: package com.sky;import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCach…...
稳定币的深度剖析与展望
一、引言 在当今数字化浪潮席卷全球的时代,加密货币作为一种新兴的金融现象,正以前所未有的速度改变着我们对传统货币和金融体系的认知。然而,加密货币市场的高度波动性却成为了其广泛应用和普及的一大障碍。在这样的背景下,稳定…...
JS设计模式(4):观察者模式
JS设计模式(4):观察者模式 一、引入 在开发中,我们经常会遇到这样的场景:一个对象的状态变化需要自动通知其他对象,比如: 电商平台中,商品库存变化时需要通知所有订阅该商品的用户;新闻网站中࿰…...
消防一体化安全管控平台:构建消防“一张图”和APP统一管理
在城市的某个角落,一场突如其来的火灾打破了平静。熊熊烈火迅速蔓延,滚滚浓烟弥漫开来,周围群众的生命财产安全受到严重威胁。就在这千钧一发之际,消防救援队伍迅速行动,而豪越科技消防一体化安全管控平台构建的消防“…...
