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使用socket向客户端发送数据的方法
Apr 29 Python
Django中处理出错页面的方法
Jul 15 Python
Python中getpass模块无回显输入源码解析
Jan 11 Python
详谈Numpy中数组重塑、合并与拆分方法
Apr 17 Python
python 读取文件并替换字段的实例
Jul 12 Python
Python3内置模块pprint让打印比print更美观详解
Jun 02 Python
利用anaconda保证64位和32位的python共存
Mar 09 Python
为什么说Python可以实现所有的算法
Oct 04 Python
python如何使用jt400.jar包代码实例
Dec 20 Python
python+opencv边缘提取与各函数参数解析
Mar 09 Python
Selenium 安装和简单使用的实现
Dec 04 Python
java关于string最常出现的面试题整理
Jan 18 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(1)
2006/10/09 PHP
xml+php动态载入与分页
2006/10/09 PHP
回答PHPCHINA上的几个问题:URL映射
2007/02/14 PHP
PHP初学者常见问题集合 修正版(21问答)
2010/03/23 PHP
php smarty truncate UTF8乱码问题解决办法
2014/06/13 PHP
PHP实现UTF-8文件BOM自动检测与移除实例
2014/11/05 PHP
php mysql获取表字段名称和字段信息的三种方法
2016/11/13 PHP
js如何判断不同系统的浏览器类型
2013/10/28 Javascript
js 页面元素的几个用法总结
2013/11/18 Javascript
JS获取URL中参数值(QueryString)的4种方法分享
2014/04/12 Javascript
jquery+ajax请求且带返回值的代码
2015/08/12 Javascript
简单谈谈node.js 版本控制 nvm和 n
2015/10/15 Javascript
理解javascript函数式编程中的闭包(closure)
2016/03/08 Javascript
快速解决jquery.touchSwipe左右滑动和垂直滚动条冲突
2016/04/15 Javascript
JS中Select下拉列表类(支持输入模糊查询)功能
2017/01/17 Javascript
node.js中grunt和gulp的区别详解
2017/07/17 Javascript
JavaScript实现鼠标滚轮控制页面图片切换功能示例
2017/10/14 Javascript
JS实现马赛克图片效果完整示例
2019/04/13 Javascript
JavaScript实现简易聊天对话框(加滚动条)
2020/02/10 Javascript
[42:20]Secret vs Liquid 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/17 DOTA
python中查找excel某一列的重复数据 剔除之后打印
2013/02/10 Python
浅析Python的Django框架中的Memcached
2015/07/23 Python
基于python yield机制的异步操作同步化编程模型
2016/03/18 Python
基于python的列表list和集合set操作
2019/11/24 Python
Windows系统下pycharm中的pip换源
2020/02/23 Python
PyCharm 2020 激活到 2100 年的教程
2020/03/25 Python
Tensorflow安装问题: Could not find a version that satisfies the requirement tensorflow
2020/04/20 Python
Python中logging日志的四个等级和使用
2020/11/17 Python
英国家庭珠宝商:T. H. Baker
2018/02/08 全球购物
办公自动化专业大学生职业规划书
2014/03/06 职场文书
说好普通话圆梦你我他演讲稿
2014/09/21 职场文书
领导班子“四风问题”“整改方案
2014/10/02 职场文书
婚前协议书范本
2014/10/27 职场文书
北京大学中文系教授推荐的10本小说
2019/08/08 职场文书
导游词之苏州盘门景区
2019/11/12 职场文书
Django路由层如何获取正确的url
2021/07/15 Python