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中使用partial改变方法默认参数实例
Apr 28 Python
python简单文本处理的方法
Jul 10 Python
详解python3中的真值测试
Aug 13 Python
python实现nao机器人手臂动作控制
Apr 29 Python
对python中基于tcp协议的通信(数据传输)实例讲解
Jul 22 Python
50行Python代码获取高考志愿信息的实现方法
Jul 23 Python
利用PyCharm操作Github(仓库新建、更新,代码回滚)
Dec 18 Python
python给指定csv表格中的联系人群发邮件(带附件的邮件)
Dec 31 Python
pandas数据拼接的实现示例
Apr 16 Python
TensorFlow使用Graph的基本操作的实现
Apr 22 Python
浅谈keras2 predict和fit_generator的坑
Jun 17 Python
python3获取控制台输入的数据的具体实例
Aug 16 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对图像的各种处理函数代码小结
2013/07/08 PHP
ThinkPHP之N方法实例详解
2014/06/20 PHP
php控制文件下载速度的方法
2015/03/24 PHP
PHP实现文件上传与下载实例与总结
2016/03/13 PHP
PHP函数checkdnsrr用法详解(Windows平台用法)
2016/03/21 PHP
php简单实现批量上传图片的方法
2016/05/09 PHP
你不知道的文件上传漏洞php代码分析
2016/09/29 PHP
laravel异步监控定时调度器实例详解
2019/06/21 PHP
jQuery之网页换肤实现代码
2011/04/30 Javascript
JS实现随机化快速排序的实例代码
2013/08/01 Javascript
JS实现pasteHTML兼容ie,firefox,chrome的方法
2016/06/22 Javascript
Bootstrap编写一个同时适用于PC、平板、手机的登陆页面
2016/06/30 Javascript
jQuery 实现批量提交表格多行数据的方法
2018/08/09 jQuery
JS与jQuery判断文本框还剩多少字符可以输入的方法
2018/09/01 jQuery
小程序云开发如何实现图片上传及发表文字
2019/05/17 Javascript
如何使用CSS3+JQuery实现悬浮墙式菜单
2019/06/18 jQuery
layui的layedit富文本赋值方法
2019/09/18 Javascript
跟老齐学Python之Import 模块
2014/10/13 Python
利用ctypes提高Python的执行速度
2016/09/09 Python
python中不能连接超时的问题及解决方法
2018/06/10 Python
Python判断字符串是否为字母或者数字(浮点数)的多种方法
2018/08/03 Python
Python设计模式之备忘录模式原理与用法详解
2019/01/15 Python
Django 1.10以上版本 url 配置注意事项详解
2019/08/05 Python
如何基于python实现年会抽奖工具
2020/10/20 Python
pandas按照列的值排序(某一列或者多列)
2020/12/13 Python
IE支持HTML5的解决方法
2009/10/20 HTML / CSS
就业协议书怎么填
2014/04/11 职场文书
公务员群众路线专题民主生活会发言材料
2014/09/17 职场文书
领导班子党的群众路线对照检查材料
2014/09/25 职场文书
消防安全主题班会
2015/08/12 职场文书
机械原理课程设计心得体会
2016/01/15 职场文书
哪类餐饮行业,最适合在高校创业?
2019/08/19 职场文书
教你使用pyinstaller打包Python教程
2021/05/27 Python
python 爬取吉首大学网站成绩单
2021/06/02 Python
解析高可用Redis服务架构分析与搭建方案
2021/06/20 Redis
自动在Windows中运行Python脚本并定时触发功能实现
2021/09/04 Python