gpt4 book ai didi

python获取本周、上周、本月、上月及本季的时间代码实例

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章python获取本周、上周、本月、上月及本季的时间代码实例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

前言 。

本文主要介绍的是关于利用python 获取本周,上周,本月,上月,本季的时间,话不多说了,来一起看看实现的方法吧 。

示例代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import datetime
from datetime import timedelta
 
now = datetime.datetime.now()
 
# 今天
today = now
print ( '--- 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 + 1
print ( '--- 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 + 1
this_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获取本周、上周、本月、上月及本季时间内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。

原文链接:https://blog.csdn.net/mmmmmm__yy/article/details/108418910 。

最后此篇关于python获取本周、上周、本月、上月及本季的时间代码实例的文章就讲到这里了,如果你想了解更多关于python获取本周、上周、本月、上月及本季的时间代码实例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com