时间:2021-05-22
步骤:
1. 掌握几种对象及其关系
2. 了解每类对象的基本操作方法
3. 通过转化关系转化
1. datetime
>>> import datetime>>> now = datetime.datetime.now()>>> nowdatetime.datetime(2018, 1, 12, 23, 9, 12, 946118)>>> type(now)<type 'datetime.datetime'>2. timestamp
>>> import time>>> time.time()1421075455.5682433. 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
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.7680756. 获取本周/本月/上月最后一天
本周
>>> today = datetime.date.today()>>> todaydatetime.date(2015, 1, 12)>>> sunday = today + datetime.timedelta(6 - today.weekday())>>> sundaydatetime.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_daydatetime.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())>>> timestamp1421077403.0timestamp -> datetime
>>> datetime.datetime.fromtimestamp(1421077403.0)datetime.datetime(2015, 1, 12, 23, 43, 23)这篇文章就介绍到这了,基本上都是python的关于日期时间操作的基础知识点,需要掌握好
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Java时间日期概要: 程序就是输入——>处理——>输出。对数据的处理是程序员需要着重注意的地方,快速、高效的对数据进行处理时我们的追求。其中,时间日期的处理
LocalDate、LocalTime、LocalDateTime是Java8开始提供的时间日期API,主要用来优化Java8以前对于时间日期的处理操作。然而,
摘要在上一篇文章,时间日期处理的入门里面,我们简单介绍了一下载pandas里对时间日期的简单操作。下面将补充一些常用方法。时间日期的比较假设我们有数据集df如下
前言我们在用Django创建models时,常常会涉及时间日期字段的处理,Django里日期相关Field有DateTimeField、DateField和Ti
简述时间日期处理是平时工作中使用非常频繁的逻辑,Java8中提供的新的时间类LocalDateTime和LocalDate,使日期处理可以更简单。友情提醒下,业