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' ) )
相关推荐
情缘晓梦.几秒前
C语言数据存储
c语言·开发语言
xunyan6234几秒前
第九章 JAVA常用类
java·开发语言
测试19981 分钟前
Web自动化测试入门
自动化测试·软件测试·python·功能测试·selenium·测试工具·测试用例
予枫的编程笔记3 分钟前
【论文解读】DLF:以语言为核心的多模态情感分析新范式 (AAAI 2025)
人工智能·python·算法·机器学习
IOT-Power9 分钟前
QT 对话框(QDialog)中 accept、reject、exec、open的使用
开发语言·qt
froginwe1111 分钟前
ASP Session
开发语言
im_AMBER12 分钟前
Leetcode 99 删除排序链表中的重复元素 | 合并两个链表
数据结构·笔记·学习·算法·leetcode·链表
YangYang9YangYan17 分钟前
中专大数据技术专业学习数据分析的价值分析
大数据·学习·数据分析
lbb 小魔仙19 分钟前
【Python】零基础学 Python 爬虫:从原理到反爬,构建企业级爬虫系统
开发语言·爬虫·python
Swift社区21 分钟前
ArkTS Web 组件里,如何通过 javaScriptProxy 让 JS 同步调用原生方法
开发语言·前端·javascript