1. import datetime
  2. from datetime import timedelta
  3. now = datetime.datetime.now()
  4. #今天
  5. today = now
  6. #昨天
  7. yesterday = now - timedelta(days=1)
  8. #明天
  9. tomorrow = now + timedelta(days=1)<br><br>#当前季度
  10. now_quarter = now.month / 3 if now.month % 3 == 0 else now.month / 3 + 1
  11. #本周第一天和最后一天
  12. this_week_start = now - timedelta(days=now.weekday())
  13. this_week_end = now + timedelta(days=6-now.weekday())
  14. #上周第一天和最后一天
  15. last_week_start = now - timedelta(days=now.weekday()+7)
  16. last_week_end = now - timedelta(days=now.weekday()+1)
  17. #本月第一天和最后一天
  18. this_month_start = datetime.datetime(now.year, now.month, 1)
  19. this_month_end = datetime.datetime(now.year, now.month + 1, 1) - timedelta(days=1)
  20. #上月第一天和最后一天
  21. last_month_end = this_month_start - timedelta(days=1)
  22. last_month_start = datetime.datetime(last_month_end.year, last_month_end.month, 1)
  23. #本季第一天和最后一天
  24. month = (now.month - 1) - (now.month - 1) % 3 + 1
  25. this_quarter_start = datetime.datetime(now.year, month, 1)
  26. this_quarter_end = datetime.datetime(now.year, month + 3, 1) - timedelta(days=1)
  27. #上季第一天和最后一天
  28. last_quarter_end = this_quarter_start - timedelta(days=1)
  29. last_quarter_start = datetime.datetime(last_quarter_end.year, last_quarter_end.month - 2, 1)
  30. #本年第一天和最后一天
  31. this_year_start = datetime.datetime(now.year, 1, 1)
  32. this_year_end = datetime.datetime(now.year + 1, 1, 1) - timedelta(days=1)
  33.  
  34. #去年第一天和最后一天
  35. last_year_end = this_year_start - timedelta(days=1)
  36. last_year_start = datetime.datetime(last_year_end.year, 1, 1)

Python取时间,日期的总结的更多相关文章

  1. python 获取日期

    转载   原文:python 获取日期 作者:m4774411wang python 获取日期我们需要用到time模块,比如time.strftime方法 time.strftime('%Y-%m-% ...

  2. python操作日期和时间的方法

    不管何时何地,只要我们编程时遇到了跟时间有关的问题,都要想到 datetime 和 time 标准库模块,今天我们就用它内部的方法,详解python操作日期和时间的方法.1.将字符串的时间转换为时间戳 ...

  3. 分别用Excel和python进行日期格式转换成时间戳格式

    最近在处理一份驾驶行为方面的数据,其中要用到时间戳,因此就在此与大家一同分享学习一下. 1.什么是时间戳? 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01 ...

  4. Python中日期和时间格式化输出的方法

    本文转自:https://www.jb51.net/article/62518.htm 本文实例总结了python中日期和时间格式化输出的方法.分享给大家供大家参考.具体分析如下: python格式化 ...

  5. 【转】Python之日期与时间处理模块(date和datetime)

    [转]Python之日期与时间处理模块(date和datetime) 本节内容 前言 相关术语的解释 时间的表现形式 time模块 datetime模块 时间格式码 总结 前言 在开发工作中,我们经常 ...

  6. python时间日期字符串各种

    python时间日期字符串各种 第一种 字符串转换成各种日期 time 库 # -*- coding: utf-8 -*- import time, datetime # 字符类型的时间 tss1 = ...

  7. Python学习---日期时间

    在Python里面日期时间的功能主要由几个模块提供:time,calendar,datetime,date等 time主要用到的功能函数: #!/usr/bin/python3 # coding:ut ...

  8. Python时间日期格式化之time与datetime模块总结

    1 引言 在实际开发过程中,我们经常会用到日期或者时间,那么在Python中我们怎么获取时间,以及如何将时间转换为我们需要的格式呢?在之前的开发中,也曾遇到time.datetime等模块下的不同函数 ...

  9. Python对日期进行格式化

    Python对日期进行格式化 把当前时间输出为2017-04-07 19:00:00.进入python交互命令行输入: > import datetime > currtime = dat ...

  10. Python:日期和时间的处理模块及相关函数

    Python:日期和时间的处理模块及相关函数 Python 提供 time 模块和 calendar 模块用于格式化日期和时间. 一.时间戳 在Python中,时间戳是以秒为单位的浮点小数,它是指格林 ...

随机推荐

  1. js关系图库:aworkflow

    auto-workflow 用于快速构建各种关系图的库,比如流程图,可视化执行流等 github地址:https://github.com/auto-workflow/AWorkflow 快速开始 n ...

  2. URAL 1430. Crime and Punishment(数论)

    题目链接 题意 :给你a,b,n,让你找出两个数x,y,使得n-(a*x+b*y)最小. 思路 : 分大小做,然后枚举a的倍数 #include <stdio.h> #include &l ...

  3. Matlab图像处理教程

    虽然典型算法的开发是基于理论支持的,但这些算法的实现几乎总是要求参数估计,并常常进行算法修正与候选求解方案的比较. MATLAB由LINPACK和EISPACK项目开发,最初用于矩阵处理.今天,MAT ...

  4. @cms_content_list

    [@cms_content_list typeId='1,2,3' count='18' orderBy='4' channelId='75' channelOption='0' dateFormat ...

  5. web_custom_request函数详解【摘抄】

    本次摘抄自:http://www.cnblogs.com/yezhaohui/p/3280239.html web_custom_request()函数是一个可以用于自定义http请求的“万能”函数, ...

  6. jquery-tmpl 插件

    做项目时页面上有处功能是:在页面有处列表.有添加,我添加修改或删除后要刷新这个列表,首先想到的是局部刷新,但我们一般说的局部刷新就是利于ajax去后台调用数据并显示,而这里是一整个列表就比较麻烦了,刷 ...

  7. asp.net mvc+webuploader大文件分片上传

    首先是前端: var GUID = WebUploader.Base.guid();//一个GUID uploadereditsVideo = WebUploader.create({ // swf文 ...

  8. 对路径“c:\windows\system32\inetsrv\syslog”的访问被拒绝。

    win7 64 系统,在调试wcf的时候,出了这个错误,当时感觉iis的权限不够,iis搞了好长时间没解决.最后改了用到的应用程序池中的标识.标识改成 localSytem,之后问题解决. IIS-- ...

  9. Windows上编译zlib

    把zlib 1.2.8解压到zlib/zlib-1.2.8 在deflate.c文件中把deflate_copyright改成一个static变量. 在zlib目录底下创建并用Visual Studi ...

  10. MVC ASP.NET MVC5使用Area区域

    MVC  ASP.NET MVC5使用Area区域 一.为什么要使用area? 在大型的ASP.NET mvc5项目中一般都有许多个功能模块,这些功能模块可以用Area(中文翻译为区域)把它们分离开来 ...