时间:2021-05-22
前言
本文主要介绍的是关于利用python 获取本周,上周,本月,上月,本季的时间,话不多说了,来一起看看实现的方法吧
示例代码:
import datetimefrom datetime import timedelta now = datetime.datetime.now() # 今天today = nowprint('--- today = {}'.format(today)) # 昨天yesterday = now - timedelta(days=1)print('--- yesterday = {}'.format(yesterday)) # 明天tomorrow = now + timedelta(days=1)print('--- tomorrow = {}'.format(tomorrow)) # 当前季度now_quarter = now.month / 3 if now.month % 3 == 0 else now.month / 3 + 1print('--- now_quarter = {}'.format(now_quarter)) # 本周第一天和最后一天this_week_start = now - timedelta(days=now.weekday())this_week_end = now + timedelta(days=6 - now.weekday())print('--- this_week_start = {} this_week_end = {}'.format(this_week_start, this_week_end)) # 上周第一天和最后一天last_week_start = now - timedelta(days=now.weekday() + 7)last_week_end = now - timedelta(days=now.weekday() + 1)print('--- last_week_start = {} last_week_end = {}'.format(last_week_start, last_week_end)) # 本月第一天和最后一天this_month_start = datetime.datetime(now.year, now.month, 1)this_month_end = datetime.datetime(now.year, now.month + 1, 1) - timedelta(days=1)+ datetime.timedelta( hours=23, minutes=59, seconds=59)print('--- this_month_start = {} this_month_end = {}'.format(this_month_start, this_month_end)) # 上月第一天和最后一天last_month_end = this_month_start - timedelta(days=1)+ datetime.timedelta( hours=23, minutes=59, seconds=59)last_month_start = datetime.datetime(last_month_end.year, last_month_end.month, 1)print('--- last_month_end = {} last_month_start = {}'.format(last_month_end, last_month_start)) # 本季第一天和最后一天month = (now.month - 1) - (now.month - 1) % 3 + 1this_quarter_start = datetime.datetime(now.year, month, 1)this_quarter_end = datetime.datetime(now.year, month + 3, 1) - timedelta(days=1)+ datetime.timedelta( hours=23, minutes=59, seconds=59)print('--- this_quarter_start = {} this_quarter_end = {}'.format(this_quarter_start, this_quarter_end)) # 上季第一天和最后一天last_quarter_end = this_quarter_start - timedelta(days=1)+ datetime.timedelta( hours=23, minutes=59, seconds=59)last_quarter_start = datetime.datetime(last_quarter_end.year, last_quarter_end.month - 2, 1)print('--- last_quarter_start = {} last_quarter_end = {}'.format(last_quarter_start, last_quarter_end)) # 本年第一天和最后一天this_year_start = datetime.datetime(now.year, 1, 1)this_year_end = datetime.datetime(now.year + 1, 1, 1) - timedelta(days=1)+ datetime.timedelta( hours=23, minutes=59, seconds=59)print('--- this_year_start = {} this_year_end = {}'.format(this_year_start, this_year_end)) # 去年第一天和最后一天last_year_end = this_year_start - timedelta(days=1)+ datetime.timedelta( hours=23, minutes=59, seconds=59)last_year_start = datetime.datetime(last_year_end.year, 1, 1)print('--- last_year_start = {} last_year_end = {}'.format(last_year_start, last_year_end))总结
到此这篇关于利用python获取本周、上周、本月、上月及本季的时间的文章就介绍到这了,更多相关python获取本周、上周、本月、上月及本季时间内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
js获取本周、上周、本月、上月、本季度、上季度的开始结束日期/***获取本周、本季度、本月、上月的开始日期、结束日期*/varnow=newDate();//当
Js代码复制代码代码如下:/***获取本周、本季度、本月、上月的开端日期、停止日期*/varnow=newDate();//当前日期varnowDayOfWee
本文实例为大家分享了vue按时间段查询的案例,效果图如下html代码今天昨天本周上周本月上月查询vue.js代码点击事件//获取时间、//时间解析;Time:f
//C#根据当前时间获取本周、下周、本月、下月、本季度等时间段DateTimedt=DateTime.Now;//当前时间DateTimestartWeek=d
本文实例讲述了PHP简单获取上月、本月、近15天、近30天方法。分享给大家供大家参考,具体如下:/***获取统计时间*@param$type*1上月*2本月*3