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 获取et和excel的版本号
Apr 09 Python
Python中的自省(反射)详解
Jun 02 Python
TensorFLow用Saver保存和恢复变量
Mar 10 Python
使用matplotlib画散点图的方法
May 25 Python
利用Python将每日一句定时推送至微信的实现方法
Aug 13 Python
Python面向对象之类和实例用法分析
Jun 08 Python
树莓派与PC端在局域网内运用python实现即时通讯
Jun 22 Python
python如何把字符串类型list转换成list
Feb 18 Python
Python HTMLTestRunner库安装过程解析
May 25 Python
keras slice layer 层实现方式
Jun 11 Python
Python descriptor(描述符)的实现
Nov 15 Python
解决pytorch 模型复制的一些问题
Mar 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
全国FM电台频率大全 - 24 贵州省
2020/03/11 无线电
在PHP中利用XML技术构造远程服务(下)
2006/10/09 PHP
一个php作的文本留言本的例子(五)
2006/10/09 PHP
网络资源
2006/10/09 PHP
php实现zip压缩文件解压缩代码分享(简单易懂)
2014/05/10 PHP
php递归实现无限分类的方法
2015/07/28 PHP
深入理解PHP 数组之count 函数
2016/06/13 PHP
PDO::errorInfo讲解
2019/01/28 PHP
php防止表单重复提交实例讲解
2019/02/11 PHP
用javascript动态调整iframe高度的代码
2007/04/10 Javascript
document.createElement()用法
2013/03/13 Javascript
使用jQuery实现的网页版的个人简历(可换肤)
2013/04/19 Javascript
js与jQuery 获取父窗、子窗的iframe
2013/12/20 Javascript
javascript实现保留两位小数的多种方法
2015/12/18 Javascript
JavaScript中的函数(二)
2015/12/23 Javascript
AngularJS入门教程之静态模板详解
2016/08/18 Javascript
URL中“#” “?” &amp;“”号的作用浅析
2017/02/04 Javascript
bootstrap fileinput实现文件上传功能
2017/08/23 Javascript
JavaScript实现手机号码 3-4-4格式并控制新增和删除时光标的位置
2020/06/02 Javascript
原生JS实现微信通讯录
2020/06/18 Javascript
[55:56]NB vs Infamous 2019国际邀请赛淘汰赛 败者组 BO3 第二场 8.22
2019/09/05 DOTA
python读写ini配置文件方法实例分析
2015/06/30 Python
PyQt5 QSerialPort子线程操作的实现
2018/04/21 Python
Django数据库连接丢失问题的解决方法
2018/12/29 Python
Python redis操作实例分析【连接、管道、发布和订阅等】
2019/05/16 Python
pytorch sampler对数据进行采样的实现
2019/12/31 Python
python实现学生信息管理系统源码
2021/02/22 Python
沙特阿拉伯网上购物:Sayidaty Mall
2018/05/06 全球购物
计算机专业推荐信范文
2013/11/20 职场文书
食堂员工工作职责
2013/12/18 职场文书
法院授权委托书格式
2014/09/28 职场文书
2015年班级元旦晚会活动总结
2014/11/28 职场文书
2015年房地产销售工作总结
2015/04/20 职场文书
任命书格式模板
2015/09/22 职场文书
Windows 11要来了?微软文档揭示Win11太阳谷 / Win10有两个不同版本
2021/11/21 数码科技
Vue实现跑马灯样式文字横向滚动
2021/11/23 Vue.js