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 ...
随机推荐
- Hadoop 格式化namenode时报错警告:WARN common.Util: Path /data/dfs/name should be specified as a URI in configuration
格式化namenode时报错警告:WARN common.Util: Path /data/dfs/name should be specified as a URI in configuration ...
- linux学习5 Linux开篇入门和基本操作
一.完整的操作系统 1.GNU系统:表示GNU is Not Unix.表示不做商业化.制定了GPL(General Public License)即任何软件程序只要遵循GPL协议就是自由软件.还制定 ...
- 自行撰写Grasshopper电池
Grasshopper目前作为参数化设计是非常常用的工具,但是人们会经常碰到它提供的电池不能满足自己设计方案需求的情况,所以就需要自己创作电池,而最简单的一种方法就是自己写. 工具: Visual S ...
- Connection: close
在http1.1中request和reponse header中都有可能出现一个connection头字段,此header的含义是当client和server通信时对于长链接如何进行处理. 在http ...
- SpringData like关键字不起作用
使用springdata简单查询时,like关键字不起作用 Hibernate: select article0_.oId as oId1_2_, article0_.articleAbstract ...
- CORS & CSP笔记
1.CORS & CSP 浏览器跨域相关的安全策略主要存在于两个方面: 浏览器是否发送ajax 浏览器是否加载返回数据 假设从a.com 向b.com发送ajax请求.此时浏览器当前页面为a. ...
- C排序算法
几个常用的排序算法:插入排序.快速排序.归并排序 #include <stdio.h> #include <stdlib.h> #include <stdbool.h&g ...
- MFC调用CEF实现单页面单COOKIE管理《转》
cookie简单介绍 cookie存储了网站的一些很重要的信息,如用户身份信息.常用设置.设置地理位置等等各种信息.使用cef访问网站时,如果设置了CefSettings.cache_path参数,则 ...
- 【转载】迄今为止把同步/异步/阻塞/非阻塞/BIO/NIO/AIO讲的这么清楚的好文章(快快珍藏)
原文链接:https://www.cnblogs.com/lixinjie/p/10811219.html 常规的误区 假设有一个展示用户详情的需求,分两步,先调用一个HTTP接口拿到详情数据,然后使 ...
- Understanding glibc malloc
https://wooyun.js.org/drops/深入理解%20glibc%20malloc.html https://sploitfun.wordpress.com/2015/02/10/un ...