PYTHON基础-时间日期处理小结
作者:mdxy-dxy 时间:2023-04-23 02:02:59
步骤:
1. 掌握几种对象及其关系
2. 了解每类对象的基本操作方法
3. 通过转化关系转化
涉及对象
1. datetime
>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2018, 1, 12, 23, 9, 12, 946118)
>>> type(now)
<type 'datetime.datetime'>
2. timestamp
>>> import time
>>> time.time()
1421075455.568243
3. time tuple
>>> import time
>>> time.localtime()
time.struct_time(tm_year=2015, tm_mon=1, tm_mday=12, tm_hour=23, tm_min=10, tm_sec=30, tm_wday=0, tm_yday=12, tm_isdst=0)
4. string
>>> import datetime
>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
'2015-01-12 23:13:08'
5. date
>>> import datetime
>>> datetime.datetime.now().date()
datetime.date(2015, 1, 12)
datetime基本操作
1. 获取当前datetime
>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2015, 1, 12, 23, 26, 24, 475680)
2. 获取当天date
>>> datetime.date.today()
datetime.date(2015, 1, 12)
3. 获取明天/前N天
明天
>>> datetime.date.today() + datetime.timedelta(days=1)
datetime.date(2015, 1, 13)
三天前
>>> datetime.datetime.now()
datetime.datetime(2015, 1, 12, 23, 38, 55, 492226)
>>> datetime.datetime.now() - datetime.timedelta(days=3)
datetime.datetime(2015, 1, 9, 23, 38, 57, 59363)
4. 获取当天开始和结束时间(00:00:00 23:59:59)
>>> datetime.datetime.combine(datetime.date.today(), datetime.time.min)
datetime.datetime(2015, 1, 12, 0, 0)
>>> datetime.datetime.combine(datetime.date.today(), datetime.time.max)
datetime.datetime(2015, 1, 12, 23, 59, 59, 999999)
5. 获取两个datetime的时间差
>>> (datetime.datetime(2015,1,13,12,0,0) - datetime.datetime.now()).total_seconds()
44747.768075
6. 获取本周/本月/上月最后一天
本周
>>> today = datetime.date.today()
>>> today
datetime.date(2015, 1, 12)
>>> sunday = today + datetime.timedelta(6 - today.weekday())
>>> sunday
datetime.date(2015, 1, 18)
本月
>>> import calendar
>>> today = datetime.date.today()
>>> _, last_day_num = calendar.monthrange(today.year, today.month)
>>> last_day = datetime.date(today.year, today.month, last_day_num)
>>> last_day
datetime.date(2015, 1, 31)
获取上个月的最后一天(可能跨年)
>>> import datetime
>>> today = datetime.date.today()
>>> first = datetime.date(day=1, month=today.month, year=today.year)
>>> lastMonth = first - datetime.timedelta(days=1)
关系转换
几个关系之间的转化
Datetime Object / String / timestamp / time tuple
关系转换例子
datetime <=> string
datetime -> string
>>> import datetime
>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
'2015-01-12 23:13:08'
string -> datetime
>>> import datetime
>>> datetime.datetime.strptime("2014-12-31 18:20:10", "%Y-%m-%d %H:%M:%S")
datetime.datetime(2014, 12, 31, 18, 20, 10)
datetime <=> timetuple
datetime -> timetuple
>>> import datetime
>>> datetime.datetime.now().timetuple()
time.struct_time(tm_year=2015, tm_mon=1, tm_mday=12, tm_hour=23, tm_min=17, tm_sec=59, tm_wday=0, tm_yday=12, tm_isdst=-1)
timetuple -> datetime
timetuple => timestamp => datetime [看后面datetime<=>timestamp]
datetime <=> date
datetime -> date
>>> import datetime
>>> datetime.datetime.now().date()
datetime.date(2015, 1, 12)
date -> datetime
>>> datetime.date.today()
datetime.date(2015, 1, 12)
>>> today = datetime.date.today()
>>> datetime.datetime.combine(today, datetime.time())
datetime.datetime(2015, 1, 12, 0, 0)
>>> datetime.datetime.combine(today, datetime.time.min)
datetime.datetime(2015, 1, 12, 0, 0)
datetime <=> timestamp
datetime -> timestamp
>>> now = datetime.datetime.now()
>>> timestamp = time.mktime(now.timetuple())
>>> timestamp
1421077403.0
timestamp -> datetime
>>> datetime.datetime.fromtimestamp(1421077403.0)
datetime.datetime(2015, 1, 12, 23, 43, 23)
来源:http://www.wklken.me/posts/2015/03/03/python-base-datetime.html#
标签:PYTHON,时间日期
0
投稿
猜你喜欢
sqlserver数据库主键的生成方式小结(sqlserver,mysql)
2012-08-21 10:25:45
Linux 中PHP的安装教程
2023-10-07 20:34:21
详解字符串在Python内部是如何省内存的
2021-04-19 08:19:42
MySQL数据库学习之排序与单行处理函数详解
2024-01-22 13:01:01
用OpenCV进行年龄和性别检测的实现示例
2021-02-17 18:18:19
使用Python paramiko模块利用多线程实现ssh并发执行操作
2021-02-02 03:10:05
一篇文章搞定数据库连接池
2024-01-19 02:55:49
python将字符串list写入excel和txt的实例
2022-02-28 05:07:01
JScript 运算符
2007-08-22 16:22:00
30万条数据,搜索文本字段的各种方式对比
2010-05-02 10:17:00
php生成缩略图填充白边(等比缩略图方案)
2024-06-05 09:50:16
详解Tensorflow数据读取有三种方式(next_batch)
2023-08-10 07:30:42
python 数据挖掘算法的过程详解
2022-11-17 09:09:19
golang validator库参数校验实用技巧干货
2024-04-23 09:38:17
跟我学习javascript的作用域与作用域链
2024-04-22 12:56:40
Java正则表达式匹配字符串并提取中间值的方法实例
2022-02-06 11:22:43
python 实现检验33品种数据是否是正态分布
2022-09-25 19:57:55
python删除csv文件的行列
2021-04-10 20:41:08
python中if嵌套命令实例讲解
2022-03-11 21:52:51
vue中的mixins混入使用方法
2023-07-02 17:01:47