# -*- 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. BigData--hadoop集群搭建之hbase安装

    之前在hadoop-2.7.3 基础上搭建hbase 详情请见:https://www.cnblogs.com/aronyao/p/hadoop.html 基础条件:先配置完成zookeeper 准备 ...

  2. Python学习 :深浅拷贝

    深浅拷贝 一.浅拷贝 只拷贝第一层数据(不可变的数据类型),并创建新的内存空间进行储蓄,例如:字符串.整型.布尔 除了字符串以及整型,复杂的数据类型都使用一个共享的内存空间,例如:列表 列表使用的是同 ...

  3. socket编程基础1——hostent、in_addr、gethostbyname、inet_ntoa

    1. struct hostent结构体 struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; ...

  4. 4368: [IOI2015]boxes纪念品盒

    4368: [IOI2015]boxes纪念品盒 链接 分析 链接 代码 #include<bits/stdc++.h> using namespace std; typedef long ...

  5. mysql 5.7.19 zip版本 windows安装步骤

    请注意此文档用于msyql5.7系列及以后版本(包括最新 mysql 8.0.11)zip版本windows下的安装1.下载mysql省略2.解压mysql到D:\Program Files\mysq ...

  6. 【数据库】 SQLite 语法

    [数据库] SQLite 语法 一 . 创建数据库 1. 只需创建数据库,只需创建文件,操作时将连接字符串指向该文件即可 2. 连接字符串 : data source = FilePath; 不能加密 ...

  7. 通过圆形按钮的绘制熟悉Qt的绘图机制,掌握这种终极方法

    基本上用QPainter就可以实现 1. QPainter painter(this); //开始的标志(可以不用) painter.begin(this); //保存最初的设置 painter.sa ...

  8. idea在Maven Projects中显示灰色的解决办法

    问题描述: 在使用idea的过程中,遇到其中一个maven模块变成灰色,如下所示: 问题解决: 造成这个的原因可能是忽略了maven模块. 可以尝试如下解决方法:在idea中进入Settings–&g ...

  9. Tuxedo 介绍与安装

    Tuxedo 介绍与安装(一) Tuxedo介绍                                                                             ...

  10. CF 55D

    Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer numb ...