Python日期和时间类型

复制代码
1 # coding = utf-8
 2
 3 from datetime import *
 4
 5 ########################## 日期 ##########################
 6 date_now = date.today()
 7 print('now:', date_now)
 8 print('year:', date_now.year)
 9 print('month:', date_now.month)
10 print('day:', date_now.day)
11
12 date_tomorrow = date_now.replace(day = date_now.day + 1)
13 print('tomorrow:', date_tomorrow)
14
15 delta = date_tomorrow - date_now
16 print('tomorrow - now:', delta)
17
18 ########################## 时间 ##########################
19 time_now = time(13, 59, 59)
20 print('now time:', time_now)
21 print('hour:', time_now.hour)
22 print('minute:', time_now.minute)
23 print('second:', time_now.second)
24 print('next hour time:', time_now.replace(hour = time_now.hour + 1))
25
26 ########################## 日期时间 ##########################
27 now = datetime.today()
28 print('date:', now.date())
29 print('time:', now.time())
30
31 ########################## 格式化 ##########################
32 print(datetime.today().strftime( '%Y-%m-%d %H:%M:%S %f' ) )
复制代码

 

posted @   午亭爱成长  阅读(365)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示