# -*- 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=""
if(int(mon)<10):
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)[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 = ""
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 ""+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获取当前日期前后N天或N月的日期的更多相关文章

  1. 【python】python获取当前日期前后N天或N月的日期

    # -*- coding: utf- -*- '''获取当前日期前后N天或N月的日期''' from time import strftime, localtime from datetime imp ...

  2. python里如何获取当前日期前后N天或N月的日期

    #!/usr/bin/python#_*_ coding:UTF-8_*_ import timeimport datetimeimport mathimport calendar ''' time. ...

  3. JAVA获取当前日期的下周一到下周日的所有日期集合

    /** * 获取当前日期的下周一到下周日的所有日期集合 * @return */ public static List getNextWeekDateList(){ Calendar cal1 = C ...

  4. JAVA获取当前日期所在的周一到周日的所有日期集合

    /** * 获取当前日期所在的周一到周日的所有日期集合 * @return */ public static List<Date> getWeekDateList() { Calendar ...

  5. python 获取当前日期 星期

    from datetime import datetime d =datetime.today()     #获取当前日期时间 d.isoweekday()           #获取时间周几

  6. python获取当前日期时间

    转载自:https://www.cnblogs.com/wenBlog/p/6023742.html 在Python里如何获取当前的日期和时间呢?在Python语言里,我们可以通过调用什么模块或者类函 ...

  7. JAVA获取当前日期指定月份后(多少个月后)的日期

    环境要求:使用jdk1.8 package com.date; import java.text.ParseException; import java.text.SimpleDateFormat; ...

  8. Python获取当前日期和日期差计算

    在Python中获取日期和时间很简单,主要使用time和datetime包 1.获取当前时间并格式化 from dateutil import rrule from datetime import d ...

  9. python获取当前日期

    今天群里一个人问了怎么获取当前时间的问题,以前接触过计算日期之差的,具体代码如下: import datetime d1=datetime.datetime(2014,3,14) d2=datetim ...

随机推荐

  1. 在VMware上安装Ubuntu14.04 , Docker

    最近在公司做了一个关于 How to use Docker to deploy a java-based website 的KT,总结如下: 准备材料(Linux+64bit) 1. Tomcat安装 ...

  2. CSS3凹凸字

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  3. C++(MFC)中WebBrowser去除3D边框的方法(实现IDocHostUIHandler接口)控制 WebBrowser 控件的外观和行为

    在 CSDN 上经常看到以下两个问题:1.在 MFC 应用程序中,如果创建了一个 WebBrowser 控件(包括 CHtmlView 在内),如何可以把该控件的三维边框禁止掉?2.在 MFC 应用程 ...

  4. hibernate学习(设计多对多 关系 映射)

    // package org.crazy.app.domain; import java.util.HashSet; import java.util.Set; import javax.persis ...

  5. Codeforces Round #243

    CF 243  DIV1 & DIV2 DIV2的A和B都太水,直接暴力搞就可以的. DIV2A /* ******************************************** ...

  6. java实现smtp邮件发送

    一.准备工作 首先你需要已一个发送邮箱,一般的邮箱都有SMTP.POP3服务,比如QQ邮箱,登陆QQ邮箱开启SMTP服务,开启是服务器会提示你设置独立密码,这个密码是跟邮箱正常登陆的密码不同的,这个是 ...

  7. eclipse 配置黑色主题

    虽然以前也使用eclipse的黑色主题,但是配置起来稍微麻烦一点. 这里先声明,下面的方式适合最新版本的Eclipse Luna,旧的版本可以下载我提供的这个插件,并将其放在eclipse目录下的pl ...

  8. 云计算和大数据时代网络技术揭秘(十三)VXLAN

    Vxlan(virtual Extensible LAN)虚拟可扩展局域网,是一种Overlay方式的网络技术,采用了mac in UDP的方式 进行封装,共50字节的报头.该技术的目标是解决虚拟机在 ...

  9. Gossip protocol(zz)

    Gossip protocol 这是一系列用于P2P的通信协议.简单来说,就是模拟人类社会中流言传播的方式.每个节点随机地把消息发给它的邻居,接到消息的节点,如果之前没收到这个消息,则会继续随机地转发 ...

  10. P1403约数研究

    洛谷1403 约数研究 题目描述 科学家们在Samuel星球上的探险得到了丰富的能源储备,这使得空间站中大型计算机"Samuel2"的长时间运算成为了可能.由于在去年一年的辛苦工作 ...