Python:日期和时间类型学习

背景

在非开发环境经常需要做一下日期计算,就准备使用Python,顺便记下来学习的痕迹。

代码

复制代码
 1  1 # coding = utf-8
 2  2 
 3  3 from datetime import *
 4  4 
 5  5 ########################## 日期 ##########################
 6  6 date_now = date.today()
 7  7 print('now:', date_now)
 8  8 print('year:', date_now.year)
 9  9 print('month:', date_now.month)
10 10 print('day:', date_now.day)
11 11 
12 12 date_tomorrow = date_now.replace(day = date_now.day + 1)
13 13 print('tomorrow:', date_tomorrow)
14 14 
15 15 delta = date_tomorrow - date_now
16 16 print('tomorrow - now:', delta)
17 17 
18 18 ########################## 时间 ##########################
19 19 time_now = time(13, 59, 59)
20 20 print('now time:', time_now)
21 21 print('hour:', time_now.hour)
22 22 print('minute:', time_now.minute)
23 23 print('second:', time_now.second)
24 24 print('next hour time:', time_now.replace(hour = time_now.hour + 1))
25 25 
26 26 ########################## 日期时间 ##########################
27 27 now = datetime.today()
28 28 print('date:', now.date())
29 29 print('time:', now.time())
30 30 
31 31 ########################## 格式化 ##########################
32 32 print(datetime.today().strftime( '%Y-%m-%d %H:%M:%S %f' ) )
相关推荐
StickToForever1 小时前
第4章 信息系统架构(五)
经验分享·笔记·学习·职场和发展
Hylan_J2 小时前
【VSCode】MicroPython环境配置
ide·vscode·python·编辑器
软件黑马王子2 小时前
C#初级教程(4)——流程控制:从基础到实践
开发语言·c#
莫忘初心丶2 小时前
在 Ubuntu 22 上使用 Gunicorn 启动 Flask 应用程序
python·ubuntu·flask·gunicorn
闲猫2 小时前
go orm GORM
开发语言·后端·golang
李白同学4 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
leegong231114 小时前
学习PostgreSQL专家认证
数据库·学习·postgresql
Moonnnn.5 小时前
51单片机学习——动态数码管显示
笔记·嵌入式硬件·学习·51单片机
黑子哥呢?5 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash
失败尽常态5235 小时前
用Python实现Excel数据同步到飞书文档
python·excel·飞书