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 相关文章推荐
python局域网ip扫描示例分享
Apr 03 Python
Python中使用Tkinter模块创建GUI程序实例
Jan 14 Python
对python for 文件指定行读写操作详解
Dec 29 Python
Python2与Python3的区别实例总结
Apr 17 Python
利用anaconda保证64位和32位的python共存
Mar 09 Python
python监控进程状态,记录重启时间及进程号的实例
Jul 15 Python
PYTHON EVAL的用法及注意事项解析
Sep 06 Python
Python多线程爬取豆瓣影评API接口
Oct 22 Python
Python socket模块方法实现详解
Nov 05 Python
Python下利用BeautifulSoup解析HTML的实现
Jan 17 Python
Django后台管理系统的图文使用教学
Jan 20 Python
Python如何用wx模块创建文本编辑器
Jun 07 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
人大复印资料处理程序_查询篇
2006/10/09 PHP
phpize的深入理解
2013/06/03 PHP
php实时倒计时功能实现方法详解
2017/02/27 PHP
Laravel 5.5基于内置的Auth模块实现前后台登陆详解
2017/12/21 PHP
文本框回车提交与禁止提交示例
2013/09/27 Javascript
Javascript中string转date示例代码
2013/11/01 Javascript
只需一行代码,轻松实现一个在线编辑器
2013/11/12 Javascript
jquery实现的一个文章自定义分段显示功能
2014/05/23 Javascript
后台获取ZTREE选中节点的方法
2015/02/12 Javascript
深入理解JavaScript系列(43):设计模式之状态模式详解
2015/03/04 Javascript
JavaScript实现MIPS乘法模拟的方法
2015/04/17 Javascript
ECMAScript5(ES5)中bind方法使用小结
2015/05/07 Javascript
jQuery+HTML5实现图片上传前预览效果
2015/08/20 Javascript
微信小程序购物商城系统开发系列-工具篇的介绍
2016/11/21 Javascript
vue.js获得当前元素的文字信息方法
2018/03/09 Javascript
JavaScript new对象的四个过程实例浅析
2018/07/31 Javascript
vue项目使用微信公众号支付总结及遇到的坑
2018/10/23 Javascript
webpack4手动搭建Vue开发环境实现todoList项目的方法
2019/05/16 Javascript
如何在JavaScript中等分数组的实现
2020/12/13 Javascript
举例讲解Python中装饰器的用法
2015/04/27 Python
详解Python中映射类型的内建函数和工厂函数
2015/08/19 Python
查看python下OpenCV版本的方法
2018/08/03 Python
几个适合python初学者的简单小程序,看完受益匪浅!(推荐)
2019/04/16 Python
在Python函数中输入任意数量参数的实例
2019/07/16 Python
python线程信号量semaphore使用解析
2019/11/30 Python
使用Jupyter notebooks上传文件夹或大量数据到服务器
2020/04/14 Python
python中如何进行连乘计算
2020/05/28 Python
python入门教程之基本算术运算符
2020/11/13 Python
python 模拟登陆163邮箱
2020/12/15 Python
HTML 5.1来了 9月份正式发布 更新内容预览
2016/04/26 HTML / CSS
Myholidays美国:在线旅游网站
2019/08/16 全球购物
Blue Nile蓝色尼罗河香港官网:世界最大在线钻石珠宝销售商
2020/05/07 全球购物
实习报告评语
2014/04/26 职场文书
经销商年会策划方案
2014/05/29 职场文书
2016春节家属慰问信
2015/03/25 职场文书
读《儒林外史》有感:少一些功利,多一些真诚
2020/01/19 职场文书