当前位置: 首页 > news >正文

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 集成定时任务(一)

第二十二章 &#xff1a; Spring Boot 集成定时任务&#xff08;一&#xff09; 前言 本章知识点&#xff1a; 介绍使用Spring Boot内置的Scheduled注解来实现定时任务-单线程和多线程&#xff1b;以及介绍Quartz定时任务调度框架&#xff1a;简单定时调度器&#xff08;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&#xff0c;再打开krita&#xff0c;出现问题提示&#xff0c; 打开 cd custom_nodes 输入命令 安装控件 git clone https://github.com/Acly/comfyui-tooling-nodes.git krita基础设置 设置模型 设置lora &#xff08;可设置lora强度 增加更多…...

云原生系列2-CICD持续集成部署-GitLab和Jenkins

1、CICD持续集成部署 传统软件开发流程&#xff1a; 1、项目经理分配模块开发任务给开发人员&#xff08;项目经理-开发&#xff09; 2、每个模块单独开发完毕&#xff08;开发&#xff09;&#xff0c;单元测试&#xff08;测试&#xff09; 3、开发完毕后&#xff0c;集成部…...

50ms时延工业相机

华睿工业相机A3504CG000 参数配置&#xff1a; 相机端到端理论时延&#xff1a;80ms 厂家同步信息&#xff0c;此款设备帧率上线23fps&#xff0c;单帧时延&#xff1a;43.48ms&#xff0c;按照一图缓存加上传输显示的话&#xff0c;厂家预估时延在&#xff1a;80ms 厂家还有…...

CPU缓存一致性问题

什么是可见性问题&#xff1f; Further Reading &#xff1a;什么是可见性问题&#xff1f; 缓存一致性 内存一致性 内存可见性 顺序一致性区别 CPU缓存一致性问题 由于CPU缓存的出现&#xff0c;很好地解决了处理器与内存速度之间的矛盾&#xff0c;极大地提高了CPU的吞吐能…...

35道HTML高频题整理(附答案背诵版)

1、简述 HTML5 新特性 &#xff1f; HTML5 是 HTML 的最新版本&#xff0c;它引入了很多新的特性和元素&#xff0c;以提供更丰富的网页内容和更好的用户体验。以下是一些主要的新特性&#xff1a; 语义元素&#xff1a;HTML5 引入了新的语义元素&#xff0c;像 <article&g…...

【powershell】Windows环境powershell 运维之历史文件压缩清理

&#x1f984; 个人主页——&#x1f390;开着拖拉机回家_Linux,大数据运维-CSDN博客 &#x1f390;✨&#x1f341; &#x1fa81;&#x1f341;&#x1fa81;&#x1f341;&#x1fa81;&#x1f341;&#x1fa81;&#x1f341; &#x1fa81;&#x1f341;&#x1fa81;&am…...

【Linux】Linux线程概念和线程控制

文章目录 一、Linux线程概念1.什么是线程2.线程的优缺点3.线程异常4.线程用途5.Linux进程VS线程 二、线程控制1.线程创建2.线程终止3.线程等待4.线程分离 一、Linux线程概念 1.什么是线程 线程是进程内的一个执行流。 我们知道&#xff0c;一个进程会有对应的PCB&#xff0c;…...

Flink cdc3.0同步实例(动态变更表结构、分库分表同步)

文章目录 前言准备flink环境docker构建mysql、doris环境数据准备 通过 FlinkCDC cli 提交任务整库同步同步变更路由变更路由表结构不一致无法同步 结尾 前言 最近Flink CDC 3.0发布&#xff0c; 不仅提供基础的数据同步能力。schema 变更自动同步、整库同步、分库分表等增强功…...

国产Apple Find My认证芯片哪里找,伦茨科技ST17H6x芯片可以帮到您

深圳市伦茨科技有限公司&#xff08;以下简称“伦茨科技”&#xff09;发布ST17H6x Soc平台。成为继Nordic之后全球第二家取得Apple Find My「查找」认证的芯片厂家&#xff0c;该平台提供可通过Apple Find My认证的Apple查找&#xff08;Find My&#xff09;功能集成解决方案。…...

肺癌相关知识

写在前面 大概想了解下肺癌相关的知识&#xff0c;开此贴做记录&#xff0c;看看后续有没有相关的生信文章思路。 综述 文章名期刊影响因子Lung cancer immunotherapy: progress, pitfalls, and promisesMol Cancer37.3 常见治疗手段有surgery, radiation therapy, chemoth…...

ChimeraX使用教程-安装及基本操作

ChimeraX使用教程-安装及基本操作 1、访问https://www.cgl.ucsf.edu/chimerax/download.html进行下载&#xff0c;然后安装 安装完成后&#xff0c;显示界面 2、基本操作 1、点击file&#xff0c;导入 .PDB 文件。 &#xff08;注&#xff1a;在 alphafold在线预测蛋白》点…...

【小黑嵌入式系统第十一课】μC/OS-III程序设计基础(一)——任务设计、任务管理(创建基本状态内部任务)、任务调度、系统函数

上一课&#xff1a; 【小黑嵌入式系统第十课】μC/OS-III概况——实时操作系统的特点、基本概念&#xff08;内核&任务&中断&#xff09;、与硬件的关系&实现 文章目录 一、任务设计1.1 任务概述1.2 任务的类型1.2.1 单次执行类任务&#xff08;运行至完成型&#…...

Redis一些常用的技术

文章目录 第1关&#xff1a;Redis 事务与锁机制第2关&#xff1a;流水线第3关&#xff1a;发布订阅第4关&#xff1a;超时命令第5关&#xff1a;使用Lua语言 第1关&#xff1a;Redis 事务与锁机制 编程要求 根据提示&#xff0c;在右侧编辑器Begin-End补充代码&#xff0c;根据…...

基于QPainter 绘图图片绕绘制设备中心旋转

项目地址:https://gitcode.com/m0_45463480/QPainter/tree/main 获取途径:进入CSDN->GitCode直接下载或者通过git拉取仓库内容。 QPainter是Qt框架中的一个类,用于在QWidget或QPixmap等设备上进行绘图操作。它提供了丰富的绘图功能,可以用于绘制线条、图形、文本等。Q…...

计算机网络(4):网络层

网络层提供的两种服务 虚电路服务&#xff08;Virtual Circuit Service&#xff09;和数据报服务&#xff08;Datagram Service&#xff09;是在网络层&#xff08;第三层&#xff09;提供的两种不同的通信服务。它们主要区别在于建立连接的方式和数据传输的方式。 虚电路服务…...

动态内存分配(malloc和free​、calloc和realloc​)

目录 一、为什么要有动态内存分配​ 二、C/C中程序内存区域划分​ 三、malloc和free​ 2.1、malloc 2.2、free​ 四、calloc和realloc​ 3.1、calloc​ 3.2、realloc​ 3.3realloc在调整内存空间的是存在两种情况&#xff1a; 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;/*制作饮品的大致流程为&#xff1a;煮水 - 冲泡 - 倒入杯中 - 加入辅料 利用多态技术实现本案例&#xff0c;提供抽象制作饮品基类&#xff0c;提供子类制作咖啡和茶叶*//*基类*/ class AbstractDr…...

调用支付宝接口响应40004 SYSTEM_ERROR问题排查

在对接支付宝API的时候&#xff0c;遇到了一些问题&#xff0c;记录一下排查过程。 Body:{"datadigital_fincloud_generalsaas_face_certify_initialize_response":{"msg":"Business Failed","code":"40004","sub_msg…...

(二)TensorRT-LLM | 模型导出(v0.20.0rc3)

0. 概述 上一节 对安装和使用有个基本介绍。根据这个 issue 的描述&#xff0c;后续 TensorRT-LLM 团队可能更专注于更新和维护 pytorch backend。但 tensorrt backend 作为先前一直开发的工作&#xff0c;其中包含了大量可以学习的地方。本文主要看看它导出模型的部分&#x…...

大数据零基础学习day1之环境准备和大数据初步理解

学习大数据会使用到多台Linux服务器。 一、环境准备 1、VMware 基于VMware构建Linux虚拟机 是大数据从业者或者IT从业者的必备技能之一也是成本低廉的方案 所以VMware虚拟机方案是必须要学习的。 &#xff08;1&#xff09;设置网关 打开VMware虚拟机&#xff0c;点击编辑…...

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 中&#xff0c;元素的定位通过 position 属性控制&#xff0c;共有 5 种定位模式&#xff1a;static&#xff08;静态定位&#xff09;、relative&#xff08;相对定位&#xff09;、absolute&#xff08;绝对定位&#xff09;、fixed&#xff08;固定定位&#xff09;和…...

SpringTask-03.入门案例

一.入门案例 启动类&#xff1a; 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…...

稳定币的深度剖析与展望

一、引言 在当今数字化浪潮席卷全球的时代&#xff0c;加密货币作为一种新兴的金融现象&#xff0c;正以前所未有的速度改变着我们对传统货币和金融体系的认知。然而&#xff0c;加密货币市场的高度波动性却成为了其广泛应用和普及的一大障碍。在这样的背景下&#xff0c;稳定…...

JS设计模式(4):观察者模式

JS设计模式(4):观察者模式 一、引入 在开发中&#xff0c;我们经常会遇到这样的场景&#xff1a;一个对象的状态变化需要自动通知其他对象&#xff0c;比如&#xff1a; 电商平台中&#xff0c;商品库存变化时需要通知所有订阅该商品的用户&#xff1b;新闻网站中&#xff0…...

消防一体化安全管控平台:构建消防“一张图”和APP统一管理

在城市的某个角落&#xff0c;一场突如其来的火灾打破了平静。熊熊烈火迅速蔓延&#xff0c;滚滚浓烟弥漫开来&#xff0c;周围群众的生命财产安全受到严重威胁。就在这千钧一发之际&#xff0c;消防救援队伍迅速行动&#xff0c;而豪越科技消防一体化安全管控平台构建的消防“…...