# -*- 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. VC++ 用setsockopt()来控制recv()与send()的超时

    在send(),recv()过程中有时由于网络状况等原因,收发不能预期进行,而设置收发超时控制: 以下是来自于网上一篇文章中的摘录,它是这样写的: ;//1秒, //设置发送超时 setsockopt ...

  2. Ajax请求SpringMVC

    @RequestMapping(value = "/loadMenu", method = RequestMethod.GET) @ResponseBody public Arra ...

  3. c#Winform控件总结

    1. C# WinForm控件.自定义控件整理(大全) (http://www.cnblogs.com/top5/archive/2010/04/29/1724039.html) 2. c#窗体控件用 ...

  4. (引用 )自动化测试报告HTMLtestrunner

    1>下载HTMLTestRunner.py文件,地址为: http://tungwaiyip.info/software/HTMLTestRunner.html   Windows平台: 将下载 ...

  5. CXF 入门:创建一个基于WS-Security标准的安全验证(CXF回调函数使用,)

    http://jyao.iteye.com/blog/1346547 注意:以下客户端调用代码中获取服务端ws实例,都是通过CXF 入门: 远程接口调用方式实现 直入正题! 以下是服务端配置 ==== ...

  6. 【随笔】ssh登录时如何直接在参数中加入登录密码

    如同apt-get安装程序时会有-y参数来避免交互输入一样,我也希望在ssh登录时能够直接附加登录密码以避免交互式输入密码这一步,网上找了找,方法很多. 比如直接通过密钥免密码登录,不过需要改动很多, ...

  7. 拓扑排序 POJ 2367

    今天网易的笔试,妹的,算法题没能A掉,虽然按照思路写了出来,但是尼玛好歹给个测试用例的格式呀,吐槽一下网易的笔试出的太烂了. 就一道算法题,比较石子重量,个人以为解法应该是拓扑排序. 就去POJ找了道 ...

  8. JSP 中文乱码显示处理解决方案

    来源: <http://blog.csdn.net/joyous/article/details/1504274> JSP 中文乱码显示处理解决方案 分类: 所有 Web前端 J2EE20 ...

  9. Android之ListView——ArrayAdapter的学习与总结

    问题:当ListView选定的ListItem视图中存在一些UI组件,如CheckBox,希望保存状态,但实际上第一次完成时发现勾选后的选项在列表往下滑再滑回去后,状态没有保存 解决过程: 1)思考后 ...

  10. 使用SqlBulkCopy, 插入整个DataTable中的所有数据到指定数据库中

    string sql=""; dbhelper.ExecuteNonQuery(sql); DataTable dt = dbhelper.GetDataTable(sql); i ...