当前位置: 首页 > 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 游戏代码的缺陷 …...

简易版抽奖活动的设计技术方案

1.前言 本技术方案旨在设计一套完整且可靠的抽奖活动逻辑,确保抽奖活动能够公平、公正、公开地进行,同时满足高并发访问、数据安全存储与高效处理等需求,为用户提供流畅的抽奖体验,助力业务顺利开展。本方案将涵盖抽奖活动的整体架构设计、核心流程逻辑、关键功能实现以及…...

在鸿蒙HarmonyOS 5中实现抖音风格的点赞功能

下面我将详细介绍如何使用HarmonyOS SDK在HarmonyOS 5中实现类似抖音的点赞功能&#xff0c;包括动画效果、数据同步和交互优化。 1. 基础点赞功能实现 1.1 创建数据模型 // VideoModel.ets export class VideoModel {id: string "";title: string ""…...

智慧工地云平台源码,基于微服务架构+Java+Spring Cloud +UniApp +MySql

智慧工地管理云平台系统&#xff0c;智慧工地全套源码&#xff0c;java版智慧工地源码&#xff0c;支持PC端、大屏端、移动端。 智慧工地聚焦建筑行业的市场需求&#xff0c;提供“平台网络终端”的整体解决方案&#xff0c;提供劳务管理、视频管理、智能监测、绿色施工、安全管…...

【位运算】消失的两个数字(hard)

消失的两个数字&#xff08;hard&#xff09; 题⽬描述&#xff1a;解法&#xff08;位运算&#xff09;&#xff1a;Java 算法代码&#xff1a;更简便代码 题⽬链接&#xff1a;⾯试题 17.19. 消失的两个数字 题⽬描述&#xff1a; 给定⼀个数组&#xff0c;包含从 1 到 N 所有…...

Spring Boot面试题精选汇总

&#x1f91f;致敬读者 &#x1f7e9;感谢阅读&#x1f7e6;笑口常开&#x1f7ea;生日快乐⬛早点睡觉 &#x1f4d8;博主相关 &#x1f7e7;博主信息&#x1f7e8;博客首页&#x1f7eb;专栏推荐&#x1f7e5;活动信息 文章目录 Spring Boot面试题精选汇总⚙️ **一、核心概…...

什么是EULA和DPA

文章目录 EULA&#xff08;End User License Agreement&#xff09;DPA&#xff08;Data Protection Agreement&#xff09;一、定义与背景二、核心内容三、法律效力与责任四、实际应用与意义 EULA&#xff08;End User License Agreement&#xff09; 定义&#xff1a; EULA即…...

R 语言科研绘图第 55 期 --- 网络图-聚类

在发表科研论文的过程中&#xff0c;科研绘图是必不可少的&#xff0c;一张好看的图形会是文章很大的加分项。 为了便于使用&#xff0c;本系列文章介绍的所有绘图都已收录到了 sciRplot 项目中&#xff0c;获取方式&#xff1a; R 语言科研绘图模板 --- sciRplothttps://mp.…...

通过MicroSip配置自己的freeswitch服务器进行调试记录

之前用docker安装的freeswitch的&#xff0c;启动是正常的&#xff0c; 但用下面的Microsip连接不上 主要原因有可能一下几个 1、通过下面命令可以看 [rootlocalhost default]# docker exec -it freeswitch fs_cli -x "sofia status profile internal"Name …...

鸿蒙(HarmonyOS5)实现跳一跳小游戏

下面我将介绍如何使用鸿蒙的ArkUI框架&#xff0c;实现一个简单的跳一跳小游戏。 1. 项目结构 src/main/ets/ ├── MainAbility │ ├── pages │ │ ├── Index.ets // 主页面 │ │ └── GamePage.ets // 游戏页面 │ └── model │ …...

前端开发者常用网站

Can I use网站&#xff1a;一个查询网页技术兼容性的网站 一个查询网页技术兼容性的网站Can I use&#xff1a;Can I use... Support tables for HTML5, CSS3, etc (查询浏览器对HTML5的支持情况) 权威网站&#xff1a;MDN JavaScript权威网站&#xff1a;JavaScript | MDN...