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 相关文章推荐
17个Python小技巧分享
Jan 23 Python
Windows下使Python2.x版本的解释器与3.x共存的方法
Oct 25 Python
Python中pygame安装方法图文详解
Nov 11 Python
Python使用matplotlib绘制多个图形单独显示的方法示例
Mar 14 Python
python版DDOS攻击脚本
Jun 12 Python
Python文件操作基础流程解析
Mar 19 Python
matplotlib 曲线图 和 折线图 plt.plot()实例
Apr 17 Python
Python使用jpype模块调用jar包过程解析
Jul 29 Python
Python 解析库json及jsonpath pickle的实现
Aug 17 Python
matplotlib设置颜色、标记、线条,让你的图像更加丰富(推荐)
Sep 25 Python
python3.7中安装paddleocr及paddlepaddle包的多种方法
Nov 27 Python
Django集成MongoDB实现过程解析
Dec 01 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
关于BIG5-HKSCS的解决方法
2007/03/20 PHP
php构造函数实例讲解
2013/11/13 PHP
PHP判断变量是否为0的方法
2014/02/08 PHP
php微信公众平台开发(一) 配置接口
2016/12/06 PHP
PHP-FPM和Nginx的通信机制详解
2019/02/01 PHP
laravel框架模型中非静态方法也能静态调用的原理分析
2019/11/23 PHP
jQuery EasyUI API 中文文档 - Panel面板
2011/09/30 Javascript
javascript计时器事件使用详解
2014/01/07 Javascript
javascript闭包的理解
2015/04/01 Javascript
JavaScript获得页面base标签中url的方法
2015/04/03 Javascript
JavaScript深度复制(deep clone)的实现方法
2016/02/19 Javascript
jquery.onoff实现简单的开关按钮功能(推荐)
2018/05/24 jQuery
react native 原生模块桥接的简单说明小结
2019/02/26 Javascript
vue从一个页面跳转到另一个页面并携带参数的解决方法
2019/08/12 Javascript
layui的select联动实现代码
2019/09/28 Javascript
简单了解JavaScript作用域
2020/07/31 Javascript
js实现tab栏切换效果
2020/08/02 Javascript
如何构建 vue-ssr 项目的方法步骤
2020/08/04 Javascript
Tensorflow之构建自己的图片数据集TFrecords的方法
2018/02/07 Python
浅谈解除装饰器作用(python3新增)
2018/10/15 Python
在Python中使用defaultdict初始化字典以及应用方法
2018/10/31 Python
python3.6连接mysql数据库及增删改查操作详解
2020/02/10 Python
python 线性回归分析模型检验标准--拟合优度详解
2020/02/24 Python
Python批量安装卸载1000个apk的方法
2020/04/10 Python
pandas数据处理之绘图的实现
2020/06/15 Python
python 实现Harris角点检测算法
2020/12/11 Python
CSS3 :not()选择器实现最后一行li去除某种css样式
2016/10/19 HTML / CSS
迪拜航空官方网站:flydubai
2017/04/20 全球购物
size?德国官方网站:英国伦敦的球鞋精品店
2018/03/17 全球购物
英国在线滑雪板和冲浪商店:The Board Basement
2020/01/11 全球购物
生物化学研究助理员求职信
2013/10/09 职场文书
小区推广策划方案
2014/06/06 职场文书
高校师德师风自我剖析材料
2014/09/29 职场文书
2014年个人业务工作总结
2014/11/17 职场文书
python实现自动化群控的步骤
2021/04/11 Python
python 使用pandas读取csv文件的方法
2022/12/24 Python