Pyhon时间参数的应用
Python获取 本周,上周,本月,上月,本季,上季,今年, 去年
# -*- coding: utf-8 -*-
# @time: 2019-05-13 17:30
import datetime
from datetime import timedelta now = datetime.datetime.now() # 今天
today = now # 昨天
yesterday = now - timedelta(days=1) # 明天
tomorrow = now + timedelta(days=1) # 当前季度
now_quarter = now.month / 3 if now.month % 3 == 0 else now.month / 3 + 1
#本周第一天和最后一天
this_week_start = now - timedelta(days=now.weekday())
this_week_end = now + timedelta(days=6-now.weekday()) # 上周第一天和最后一天
last_week_start = now - timedelta(days=now.weekday()+7)
last_week_end = now - timedelta(days=now.weekday()+1) # 本月第一天和最后一天
this_month_start = datetime.datetime(now.year, now.month, 1)
this_month_end = datetime.datetime(now.year, now.month + 1, 1) - timedelta(days=1) # 上月第一天和最后一天
last_month_end = this_month_start - timedelta(days=1)
last_month_start = datetime.datetime(last_month_end.year, last_month_end.month, 1) # 本季第一天和最后一天
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) # 上季第一天和最后一天
last_quarter_end = this_quarter_start - timedelta(days=1)
last_quarter_start = datetime.datetime(last_quarter_end.year, last_quarter_end.month - 2, 1) # 本年第一天和最后一天
this_year_start = datetime.datetime(now.year, 1, 1)
this_year_end = datetime.datetime(now.year + 1, 1, 1) - timedelta(days=1) # 去年第一天和最后一天
last_year_end = this_year_start - timedelta(days=1)
last_year_start = datetime.datetime(last_year_end.year, 1, 1)
关注公众号:

Pyhon时间参数的应用的更多相关文章
- SpringMVC中向服务器传递时间参数时出现的问题
1. 问题描述: 今天在SpringMVC应用中上传参数的时候遇到如下问题: The request sent by the client was syntactically incorrect 这说 ...
- Birt时间参数添加My97日历控件
首先,思路: 引用My97.js然后为时间参数的textbox添加onclick事件 1.将My97添加到项目中的webcontent目录下(如图:) 2.添加My97引用 在项目路径下找到该文件\w ...
- setInterval()的时间参数无法随参数的变化而变化
2017-04-18 写了个随机抽奖的小案例,打算随机跳动十次,每次变化的时间越来越长,也就是跳动的速度越来越慢,结果发现setInterval的时间参数并不会随着变化. <!--案例的结构如下 ...
- [转帖] Linux 时间参数
Linux下文件的三个时间参数 https://blog.csdn.net/GGxiaobai/article/details/53609478 想用touch修改创建时间呢 发现不知道怎么修改来着. ...
- ARM裸板开发:07_IIC 通过IIC总线接口读写时钟芯片时间参数实现的总结
问题一:程序直接在iRAM内部可正常执行,而程序搬移(Nand ->SDRAM)之后,就不能正常运行了 #define NAND_SECTOR_SIZE 2048 /* 读函数 */ void ...
- stat:查看文件时间参数
Linux 系统中,每个文件主要拥有 3 个时间参数,分别是文件的访问时间.数据修改时间以及状态修改时间: 访问时间(Access Time,简称 atime):只要文件的内容被读取,访问时间就会更新 ...
- pyhon时间输出
参考博客:http://www.cnblogs.com/xisheng/p/7634125.html http://www.cnpythoner.com/post/89.html 有些时候想要输出,但 ...
- SpringBoot时间参数处理完整解决方案
在JavaWeb程序的开发过程中,接口是前后端对接的主要窗口,而接口参数的接收有时候是一个令人头疼的事情,这其中最困扰程序猿的,应该是时间参数的接收. 比如:设置一个用户的过期时间,前端到底以什么格式 ...
- 输入时间参数获取rds备份集信息
1.脚本 [root@localhost tmp]# more geturl_test.py #!/usr/bin/env python #coding=utf- import os, json, u ...
随机推荐
- Oracle instr() 字符查找函数
instr()函数的格式 (俗称:字符查找函数) 格式一:instr( string1, string2 ) / instr(源字符串, 目标字符串) 格式二:instr( string1 ...
- SSH远程连接虚拟机,将虚拟机映射本地端口
本周学习内容: 1.继续学习了网络是怎么连接的和JavaScript的内容: 2.使用JavaScript实现在页面打印九九乘法表,将编写的乘法表部署到本地IIS服务器: 3.安装sshd服务,使用S ...
- Solution
小五的游戏 小碎骨的子集 芙兰朵露的框框 ⑨要求和
- 43、内置函数及每日uv、销售额统计案例
一.spark1.5内置函数 在Spark 1.5.x版本,增加了一系列内置函数到DataFrame API中,并且实现了code-generation的优化.与普通的函数不同,DataFrame的函 ...
- CF1163E Magical Permutation【线性基,构造】
题目描述:输入一个大小为\(n\)的正整数集合\(S\),求最大的\(x\),使得能构造一个\(0\)到\(2^x-1\)的排列\(p\),满足\(p_i\oplus p_{i+1}\in S\) 数 ...
- golang-切片copy
package main import ( "fmt" ) func main() { fmt.Println("-------------") data := ...
- 第12组 Alpha冲刺(1/6)
Header 队名:To Be Done 组长博客 作业博客 团队项目进行情况 燃尽图(组内共享) 展示Git当日代码/文档签入记录(组内共享) 注: 由于GitHub的免费范围内对多人开发存在较多限 ...
- Android中活动被回收了怎么办
当一个活动进入到了停止状态,是有可能被系统回收的.按下返回键的时候,活动被重新创建一次,但是里面的数据就没办法重现: 这时Activity中提供了一个onSaveInstanceState()回调方法 ...
- Pycon 北京2019
- JVM(一) ---JVM的数据模型
JVM的逻辑内存模型图 [逻辑内存模型图] ----- JVM内部分区 其实JVM内部不仅仅只有栈和堆 包括 程序计数器 . Java 虚拟机栈 .本地方法栈.Java 堆.方法区等 1. 程序计数器 ...