# -*- 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()

【python】python获取当前日期前后N天或N月的日期的更多相关文章

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

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

  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. JAVA获取当前日期指定月份后(多少个月后)的日期

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

  6. Python中获取当前日期的格式

    在Python里如何获取当前的日期和时间呢?在Python语言里,我们可以通过调用什么模块或者类函数来得到当前的时间或日期呢? 当然你可以使用时间模块(time module),该模块提供了各种和时间 ...

  7. python中获取当前日期在当月是第几天

  8. JAVA获取当前日期时间所在周的周一和周日日期

    /** * 获取当前时间所在周的周一和周日的日期时间 * @return */ public static Map<String,String> getWeekDate() { Map&l ...

  9. js获取当前日期、前一天、后一天的日期的例子

    <script> function addByTransDate(dateParameter, num) { var translateDate = "", dateS ...

随机推荐

  1. LeetCode 相交链表

    基本思路 先计算出两个链表的长度 O(n) 将长的一个链表的指示指针移动到和短链表相同长度 O(n) 两个链表指示指针同时向前移动,直到二者相同或者NULL 代码实现 /** * Definition ...

  2. jquery点击li 获取当前父节点所在类的索引

    jquery点击li 获取当前父节点所在类的索引 $('.jbcz').find('.content li').click(function(){ //alert($('.jbcz').find('. ...

  3. QQ兴趣部落 大批量引流实战技巧

    兴趣部落,犹如pc端贴吧,除去盔甲,几乎大同小异. 在文章<QQ运动,新楛的马桶还在香,营销人不应摒弃>中,阿力推推对稍微僻静的平台做过简述,和QQ运动一样,兴趣部落稍显“僻静”,执行到位 ...

  4. git克隆出错 github clone Permission denied (publickey) fatal Could not read from remote repo

    原文网址:http://blog.csdn.net/feeling450/article/details/53067563 github clone "Permission denied ( ...

  5. 修复网站漏洞对phpmyadmin防止被入侵提权的解决办法

    phpmyadmin是很多网站用来管理数据库的一个系统,尤其是mysql数据库管理的较多一些,最近phpmysql爆出漏洞,尤其是弱口令,sql注入漏洞,都会导致mysql的数据账号密码被泄露,那么如 ...

  6. 分支push不上去的问题

    还原一下现场,我在自己的项目里面,从master里面checkout的一个分支,当我在我这个分支里面进行 push代码的操作,我突然发现我的代码不能执行push的操作,如图 这个原因是由于远端的仓库没 ...

  7. Notepad++删除空行的多种实现办法

    Notepad++支持基础的正则表达式,同时由于自身丰富的插件和功能,所以删除空行或有空格的空行,有多种实现办法,条条大路通罗马,闪电博客抛砖引玉,供大家参考. 一.删除空行(不包括有空格类符号的空行 ...

  8. 基于Mysql-Proxy实现Mysql的主从复制以及读写分离(上)

    基于Mysql-Proxy实现Mysql的主从复制以及读写分离(上) 上周BOSS给分配任务让实现一下Mysql数据库的主从复制以及读写分离,然后花了一盏茶的功夫进行了调研,发现主从复制数据库进行一番 ...

  9. unresolved symbol @__security_check_cookie 解决方法

    ntstrsafe.lib(output.obj) : error LNK2019: unresolved external symbol @__security_check_cookie@4 ref ...

  10. Java Set集合(HashSet、TreeSet)

    什么是HashSet?操作过程是怎么样的? 1.HashSet底层实际上是一个HashMap,HashMap底层采用了哈希表数据结构 2.哈希表又叫做散列表,哈希表底层是一个数组,这个数组中每一个元素 ...