python 日期操作类代码


Posted in Python onMay 05, 2018

完整代码

# -*- coding: utf-8 -*-

'''获取当前日期前后N天或N月的日期'''

from time import strftime, localtime
from datetime import timedelta, date
import calendar

year = strftime("%Y",localtime())
mon = strftime("%m",localtime())
day = strftime("%d",localtime())
hour = strftime("%H",localtime())
min = strftime("%M",localtime())
sec = strftime("%S",localtime())

def today():
 '''''
 get today,date format="YYYY-MM-DD"
 '''''
 return date.today()

def todaystr():
 '''
 get date string, date format="YYYYMMDD"
 '''
 return year+mon+day

def datetime():
 '''''
 get datetime,format="YYYY-MM-DD HH:MM:SS"
 '''
 return strftime("%Y-%m-%d %H:%M:%S",localtime())

def datetimestr():
 '''''
 get datetime string
 date format="YYYYMMDDHHMMSS"
 '''
 return year+mon+day+hour+min+sec

def get_day_of_day(n=0):
 '''''
 if n>=0,date is larger than today
 if n<0,date is less than today
 date format = "YYYY-MM-DD"
 '''
 if(n<0):
  n = abs(n)
  return date.today()-timedelta(days=n)
 else:
  return date.today()+timedelta(days=n)

def get_days_of_month(year,mon): 
 ''''' 
 get days of month 
 ''' 
 return calendar.monthrange(year, mon)[1] 
 
def get_firstday_of_month(year,mon): 
 ''''' 
 get the first day of month 
 date format = "YYYY-MM-DD" 
 ''' 
 days="01" 
 if(int(mon)<10): 
  mon = "0"+str(int(mon)) 
 arr = (year,mon,days) 
 return "-".join("%s" %i for i in arr) 
 
def get_lastday_of_month(year,mon): 
 ''''' 
 get the last day of month 
 date format = "YYYY-MM-DD" 
 ''' 
 days=calendar.monthrange(year, mon)[1] 
 mon = addzero(mon) 
 arr = (year,mon,days) 
 return "-".join("%s" %i for i in arr) 
 
def get_firstday_month(n=0): 
 ''''' 
 get the first day of month from today 
 n is how many months 
 ''' 
 (y,m,d) = getyearandmonth(n) 
 d = "01" 
 arr = (y,m,d) 
 return "-".join("%s" %i for i in arr) 
 
def get_lastday_month(n=0): 
 ''''' 
 get the last day of month from today 
 n is how many months 
 ''' 
 return "-".join("%s" %i for i in getyearandmonth(n)) 
 
def getyearandmonth(n=0): 
 ''''' 
 get the year,month,days from today 
 befor or after n months 
 ''' 
 thisyear = int(year) 
 thismon = int(mon) 
 totalmon = thismon+n 
 if(n>=0): 
  if(totalmon<=12): 
   days = str(get_days_of_month(thisyear,totalmon)) 
   totalmon = addzero(totalmon) 
   return (year,totalmon,days) 
  else: 
   i = totalmon/12 
   j = totalmon%12 
   if(j==0): 
    i-=1 
    j=12 
   thisyear += i 
   days = str(get_days_of_month(thisyear,j)) 
   j = addzero(j) 
   return (str(thisyear),str(j),days) 
 else: 
  if((totalmon>0) and (totalmon<12)): 
   days = str(get_days_of_month(thisyear,totalmon)) 
   totalmon = addzero(totalmon) 
   return (year,totalmon,days) 
  else: 
   i = totalmon/12 
   j = totalmon%12 
   if(j==0): 
    i-=1 
    j=12 
   thisyear +=i 
   days = str(get_days_of_month(thisyear,j)) 
   j = addzero(j) 
   return (str(thisyear),str(j),days) 
 
def addzero(n): 
 ''''' 
 add 0 before 0-9 
 return 01-09 
 ''' 
 nabs = abs(int(n)) 
 if(nabs<10): 
  return "0"+str(nabs) 
 else: 
  return nabs 

def get_today_month(n=0): 
 ''''' 
 获取当前日期前后N月的日期
 if n>0, 获取当前日期前N月的日期
 if n<0, 获取当前日期后N月的日期
 date format = "YYYY-MM-DD" 
 ''' 
 (y,m,d) = getyearandmonth(n) 
 arr=(y,m,d) 
 if(int(day)<int(d)): 
  arr = (y,m,day) 
 return "-".join("%s" %i for i in arr) 
 

if __name__=="__main__":
 print today() 
 print todaystr()
 print datetime()
 print datetimestr()
 print get_day_of_day(20)
 print get_day_of_day(-3)
 print get_today_month(-3)
 print get_today_month(3)

这篇关于python 日期操作类的文章就介绍到这,里面涉及了python日期操作的一些基础知识。

Python 相关文章推荐
把项目从Python2.x移植到Python3.x的经验总结
Apr 20 Python
Python实现的最近最少使用算法
Jul 10 Python
python3+PyQt5使用数据库表视图
Apr 24 Python
Python学习_几种存取xls/xlsx文件的方法总结
May 03 Python
django框架自定义模板标签(template tag)操作示例
Jun 24 Python
Python高级特性 切片 迭代解析
Aug 23 Python
Flask框架 CSRF 保护实现方法详解
Oct 30 Python
Python模块相关知识点小结
Mar 09 Python
Python API 操作Hadoop hdfs详解
Jun 06 Python
如何利用python读取micaps文件详解
Oct 18 Python
Django中如何用xlwt生成表格的方法步骤
Jan 31 Python
用60行代码实现Python自动抢微信红包
Feb 04 Python
Python批量发送post请求的实现代码
May 05 #Python
PyQt5 pyqt多线程操作入门
May 05 #Python
详解pyqt5 动画在QThread线程中无法运行问题
May 05 #Python
python中in在list和dict中查找效率的对比分析
May 04 #Python
Django如何配置mysql数据库
May 04 #Python
Python实现求一个集合所有子集的示例
May 04 #Python
python list是否包含另一个list所有元素的实例
May 04 #Python
You might like
CI框架常用方法小结
2016/05/17 PHP
PHP设计模式之状态模式定义与用法详解
2018/04/02 PHP
JavaScript window.location对象
2014/11/14 Javascript
jQuery实现锚点scoll效果实例分析
2015/03/10 Javascript
EasyUI为Numberbox添加blur事件的方法
2017/03/05 Javascript
js/jq仿window文件夹框选操作插件
2017/03/08 Javascript
深入理解AngularJs-scope的脏检查(一)
2017/06/19 Javascript
微信小程序开发之animation循环动画实现的让云朵飘效果
2017/07/14 Javascript
微信小程序 本地图片按照屏幕尺寸处理
2017/08/04 Javascript
Vue结合SignalR实现前后端实时消息同步
2017/09/19 Javascript
vue router自动判断左右翻页转场动画效果
2017/10/10 Javascript
浅谈vue,angular,react数据双向绑定原理分析
2017/11/28 Javascript
JS实现利用两个队列表示一个栈的方法
2017/12/13 Javascript
实例详解Node.js 函数
2018/06/10 Javascript
Vue中 key keep-alive的实现原理
2018/09/18 Javascript
vue自定义全局共用函数详解
2018/09/18 Javascript
layui清除radio的选中状态实例
2019/11/14 Javascript
[50:24]VGJ.S vs Pain 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/20 DOTA
Python的另外几种语言实现
2015/01/29 Python
Python压缩和解压缩zip文件
2015/02/14 Python
python中list常用操作实例详解
2015/06/03 Python
python中如何正确使用正则表达式的详细模式(Verbose mode expression)
2017/11/08 Python
python中利用h5py模块读取h5文件中的主键方法
2018/06/05 Python
Django模板语言 Tags使用详解
2019/09/09 Python
Python拼接字符串的7种方式详解
2020/03/19 Python
django前端页面下拉选择框默认值设置方式
2020/08/09 Python
基于Python爬取51cto博客页面信息过程解析
2020/08/25 Python
python IP地址转整数
2020/11/20 Python
CSS3轻松实现清新 Loading 效果的简单实例
2016/06/06 HTML / CSS
html如何对span设置宽度
2019/10/30 HTML / CSS
教师应聘自荐信范文
2014/03/14 职场文书
取保候审保证书
2014/04/30 职场文书
小学优秀班干部事迹材料
2014/05/25 职场文书
环保项目建议书
2014/08/26 职场文书
2015年初中教务处工作总结
2015/07/21 职场文书
整理Python中常用的conda命令操作
2021/06/15 Python