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 splitlines使用技巧
Sep 06 Python
Python操作RabbitMQ服务器实现消息队列的路由功能
Jun 29 Python
Python上下文管理器和with块详解
Sep 09 Python
Python语言实现百度语音识别API的使用实例
Dec 13 Python
Python使用Matplotlib实现Logos设计代码
Dec 25 Python
python 通过xml获取测试节点和属性的实例
Mar 31 Python
opencv python 基于KNN的手写体识别的实例
Aug 03 Python
python 实现敏感词过滤的方法
Jan 21 Python
python入门之基础语法学习笔记
Feb 08 Python
Python爬虫程序架构和运行流程原理解析
Mar 09 Python
详解python polyscope库的安装和例程
Nov 13 Python
什么是Python装饰器?如何定义和使用?
Apr 11 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引用计数器进行垃圾收集机制介绍
2012/09/19 PHP
php中的Base62类(适用于数值转字符串)
2013/08/12 PHP
Destoon旺旺无法正常显示,点击提示“会员名不存在”的解决办法
2014/06/21 PHP
Ubuntu12下编译安装PHP5.3开发环境
2015/03/27 PHP
php eval函数一句话木马代码
2015/05/21 PHP
PHP中foreach()用法汇总
2015/07/02 PHP
PHP CURL或file_get_contents获取网页标题的代码及两者效率的稳定性问题
2015/11/30 PHP
实例详解PHP中html word 互转的方法
2016/01/28 PHP
php版交通银行网银支付接口开发入门教程
2016/09/26 PHP
zeroclipboard复制到剪切板的flash
2010/08/04 Javascript
HTML颜色选择器实现代码
2010/11/23 Javascript
JQuery扩展插件Validate 2通过参数设置验证规则
2011/09/05 Javascript
关于JS控制代码暂停的实现方法分享
2012/10/11 Javascript
开发 Internet Explorer 右键功能表(ContextMenu)
2013/07/03 Javascript
基于javascript实现单选及多选的向右和向左移动实例
2015/07/25 Javascript
JavaScript实现将数组数据添加到Select下拉框的方法
2015/08/21 Javascript
JavaScript之RegExp_动力节点Java学院整理
2017/06/29 Javascript
js对象实例详解(JavaScript对象深度剖析,深度理解js对象)
2017/09/21 Javascript
浅谈vue-router 路由传参的方法
2017/12/27 Javascript
NodeJS 中Stream 的基本使用
2018/07/30 NodeJs
亲自动手实现vue日历控件
2019/06/26 Javascript
javascript 模块依赖管理的本质深入详解
2020/04/30 Javascript
[06:33]3.19 DOTA2发布会 海涛、冷冷、2009见证希望
2014/03/21 DOTA
[00:16]热血竞技场
2019/03/06 DOTA
详解在Python程序中自定义异常的方法
2015/10/16 Python
利用python打印出菱形、三角形以及矩形的方法实例
2017/08/08 Python
对python抓取需要登录网站数据的方法详解
2018/05/21 Python
python利用requests库进行接口测试的方法详解
2018/07/06 Python
python中count函数简单用法
2020/01/05 Python
Python爬虫爬取、解析数据操作示例
2020/03/27 Python
python基于opencv 实现图像时钟
2021/01/04 Python
印度尼西亚最好的小工具在线商店:Erafone.com
2019/03/26 全球购物
编码转换,怎样实现将GB2312编码的字符串转换为ISO-8859-1编码的字符串
2014/01/07 面试题
通信工程专业个人找工作求职信范文
2013/09/21 职场文书
营销部内勤岗位职责
2014/04/30 职场文书
办公室主任四风问题对照检查材料思想汇报
2014/09/28 职场文书