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文件中文编码问题
Nov 22 Python
解决python3在anaconda下安装caffe失败的问题
Jun 15 Python
Python基于回溯法子集树模板解决取物搭配问题实例
Sep 02 Python
Python 保存矩阵为Excel的实现方法
Jan 28 Python
python3.4+pycharm 环境安装及使用方法
Jun 13 Python
Python 中list ,set,dict的大规模查找效率对比详解
Oct 11 Python
在keras中获取某一层上的feature map实例
Jan 24 Python
Pygame的程序开始示例代码
May 07 Python
Python3爬虫中关于Ajax分析方法的总结
Jul 10 Python
常用的10个Python实用小技巧
Aug 10 Python
Python Merge函数原理及用法解析
Sep 16 Python
python中添加模块导入路径的方法
Feb 03 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 CURL模拟GET及POST函数代码
2010/04/25 PHP
支持中文的PHP按字符串长度分割成数组代码
2015/05/17 PHP
ThinkPHP实现递归无级分类――代码少
2015/07/29 PHP
header与缓冲区之间的深层次分析
2016/07/30 PHP
一个加密JavaScript的开源工具PACKER2.0.2
2006/11/04 Javascript
在textarea中屏蔽js的某个function的javascript代码
2007/04/20 Javascript
javascript 一些用法小结
2009/09/11 Javascript
js parentElement和offsetParent之间的区别
2010/03/23 Javascript
node.js中watch机制详解
2014/11/17 Javascript
JavaScript比较两个对象是否相等的方法
2015/02/06 Javascript
JS实现图片放大镜效果的方法
2015/02/27 Javascript
JavaScript必知必会(十) call apply bind的用法说明
2016/06/08 Javascript
JavaScript蒙板(model)功能的简单实现代码
2016/08/04 Javascript
微信小程序点击控件修改样式实例详解
2017/07/07 Javascript
VUE中使用Vue-resource完成交互
2017/07/21 Javascript
JavaScript中的FileReader图片预览上传功能实现代码
2017/07/24 Javascript
vue中使用cropperjs的方法
2018/03/01 Javascript
vue-router实现嵌套路由的讲解
2019/01/19 Javascript
简单了解Javscript中兄弟ifream的方法调用
2019/06/17 Javascript
微信小程序基于Taro的分享图片功能实践详解
2019/07/12 Javascript
vue输入节流,避免实时请求接口的实例代码
2019/10/30 Javascript
详解JavaScript匿名函数和闭包
2020/07/10 Javascript
[09:40]DAC2018 4.5 SOLO赛 MidOne vs Miracle
2018/04/06 DOTA
Python中apply函数的用法实例教程
2014/07/31 Python
python在ubuntu中的几种安装方法(小结)
2017/12/08 Python
python邮件发送smtplib使用详解
2020/06/16 Python
详解在python操作数据库中游标的使用方法
2019/11/12 Python
python Jupyter运行时间实例过程解析
2019/12/13 Python
使用 Python 处理3万多条数据只要几秒钟
2020/01/19 Python
Python Django2 model 查询介绍(条件、范围、模糊查询)
2020/03/16 Python
matplotlib 画动态图以及plt.ion()和plt.ioff()的使用详解
2021/01/05 Python
python链表类中获取元素实例方法
2021/02/23 Python
广播电视新闻学专业应届生求职信
2013/10/08 职场文书
出国签证在职证明范本
2014/11/24 职场文书
寻找成龙观后感
2015/06/12 职场文书
《分一些蚊子进来》读后感3篇
2020/01/09 职场文书