# -*- coding: utf- -*-

 '''获取当前日期前后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=):
'''''
if n>=,date is larger than today
if n<,date is less than today
date format = "YYYY-MM-DD"
'''
if(n<):
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)[] def get_firstday_of_month(year,mon):
'''''
get the first day of month
date format = "YYYY-MM-DD"
'''
days=""
if(int(mon)<):
mon = ""+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)[]
mon = addzero(mon)
arr = (year,mon,days)
return "-".join("%s" %i for i in arr) def get_firstday_month(n=):
'''''
get the first day of month from today
n is how many months
'''
(y,m,d) = getyearandmonth(n)
d = ""
arr = (y,m,d)
return "-".join("%s" %i for i in arr) def get_lastday_month(n=):
'''''
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=):
'''''
get the year,month,days from today
befor or after n months
'''
thisyear = int(year)
thismon = int(mon)
totalmon = thismon+n
if(n>=):
if(totalmon<=):
days = str(get_days_of_month(thisyear,totalmon))
totalmon = addzero(totalmon)
return (year,totalmon,days)
else:
i = totalmon/
j = totalmon%
if(j==):
i-=
j=
thisyear += i
days = str(get_days_of_month(thisyear,j))
j = addzero(j)
return (str(thisyear),str(j),days)
else:
if((totalmon>) and (totalmon<)):
days = str(get_days_of_month(thisyear,totalmon))
totalmon = addzero(totalmon)
return (year,totalmon,days)
else:
i = totalmon/
j = totalmon%
if(j==):
i-=
j=
thisyear +=i
days = str(get_days_of_month(thisyear,j))
j = addzero(j)
return (str(thisyear),str(j),days) def addzero(n):
'''''
add before -
return -
'''
nabs = abs(int(n))
if(nabs<):
return ""+str(nabs)
else:
return nabs def get_today_month(n=):
'''''
获取当前日期前后N月的日期
if n>, 获取当前日期前N月的日期
if n<, 获取当前日期后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()
print get_day_of_day(-)
print get_today_month(-)
print get_today_month()

最新文章

  1. Java集合专题总结(1):HashMap 和 HashTable 源码学习和面试总结
  2. nodejs 的ajax获取数据express
  3. 基于html5实现的愤怒的小鸟网页游戏
  4. AIX性能监控topas命令的详细解析
  5. oracle用户权限的问题
  6. Qemu之Network Device全虚拟方案二:虚拟网卡的创建
  7. 杨晨露 Java 第一周总结
  8. SQL解决时间段重复合并问题
  9. oldboy s21day12.设计商城系统,主要提供两个功能:商品管理、会员管理。
  10. 3.ansible-iventory的写法和基本变量
  11. web字体的设置
  12. datatime
  13. java日期相关
  14. 【.NetCore学习】ubuntu16.04 搭建.net core mvc api 运行环境
  15. python记录_day15 面向对象初识
  16. Ubuntu下配置PHP和CakePHP记录
  17. RxSwift学习笔记2:Observable/生命周期/Event/oneNext/onError/onCompleted/
  18. javascript 数组对象及其方法
  19. 洛谷P4831 Scarlet loves WenHuaKe
  20. 页面获取不到spring实例化services解决方法

热门文章

  1. 线程池的类型以及执行线程submit()和execute()的区别
  2. org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field xxx exceeds its maximum permitted size of 1048576 bytes.
  3. Docker 入坑教程笔记
  4. STM32(7)——通用定时器PWM输出
  5. 网络基础,tpc,udp
  6. array_x
  7. error:control reaches end of non-void function [-Werror=return-type]
  8. Redis的自从复制(Master/Slave)
  9. HDU1209:Clock
  10. ABAP CDS ON HANA-(1)CDSビュー作成