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实现的简单hangman游戏实例
Jun 28 Python
python生成式的send()方法(详解)
May 08 Python
Python初学者需要注意的事项小结(python2与python3)
Sep 26 Python
解决PyCharm的Python.exe已经停止工作的问题
Nov 29 Python
对python中的six.moves模块的下载函数urlretrieve详解
Dec 19 Python
详解Python3 对象组合zip()和回退方式*zip
May 15 Python
基于Python获取docx/doc文件内容代码解析
Feb 17 Python
浅谈Python线程的同步互斥与死锁
Mar 22 Python
在pycharm中关掉ipython console/PyDev操作
Jun 09 Python
如何卸载python插件
Jul 08 Python
python调用私有属性的方法总结
Jul 24 Python
Python软件包安装的三种常见方法
Jul 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
一贴学会PHP 新手入门教程
2009/08/03 PHP
PHP中基于ts与nts版本- vc6和vc9编译版本的区别详解
2013/04/26 PHP
深入PHP异步执行的详解
2013/06/03 PHP
php初始化对象和析构函数的简单实例
2014/03/11 PHP
php编写的一个E-mail验证类
2015/03/25 PHP
基于jQuery的获得各种控件Value的方法
2010/11/19 Javascript
JS判断两个时间大小的示例代码
2014/01/28 Javascript
JS实现很酷的EMAIL地址添加功能实例
2015/02/28 Javascript
JavaScript精炼之构造函数 Constructor及Constructor属性详解
2015/11/05 Javascript
Bootstrap每天必学之按钮(一)
2015/11/24 Javascript
JavaScript中三种异步上传文件方式
2016/03/06 Javascript
Angular.js与Bootstrap相结合实现表格分页代码
2016/04/12 Javascript
AngularJS过滤器filter用法总结
2016/12/13 Javascript
JS 组件系列之Bootstrap Table的冻结列功能彻底解决高度问题
2017/06/30 Javascript
js数组去重的N种方法(小结)
2018/06/07 Javascript
原生js实现公告滚动效果
2021/01/10 Javascript
js实现网页同时进行多个倒计时功能
2019/02/25 Javascript
详解微信小程序实现跑马灯效果(附完整代码)
2019/04/29 Javascript
微信小程序绑定手机号获取验证码功能
2019/10/22 Javascript
JS实现判断移动端PC端功能
2020/02/21 Javascript
解决elementui表格操作列自适应列宽
2020/12/28 Javascript
python根据日期返回星期几的方法
2015/07/06 Python
python获取程序执行文件路径的方法(推荐)
2018/04/26 Python
Python爬虫爬取煎蛋网图片代码实例
2019/12/16 Python
使用Python实现牛顿法求极值
2020/02/10 Python
全球最大化妆品零售网站:SkinStore
2020/10/24 全球购物
副总经理工作职责
2013/11/28 职场文书
四年级科学教学反思
2014/02/10 职场文书
《争吵》教学反思
2014/02/15 职场文书
寄语学生的话
2014/04/10 职场文书
党委班子剖析材料
2014/08/21 职场文书
2014年教师业务工作总结
2014/12/19 职场文书
龙潭大峡谷导游词
2015/02/10 职场文书
无婚姻登记记录证明
2015/06/18 职场文书
「月刊Action」2022年5月号封面公开
2022/03/21 日漫
Win11局域网共享权限在哪里设置? Win11高级共享的设置技巧
2022/04/05 数码科技