【python】python获取当前日期前后N天或N月的日期
# -*- 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月的日期的更多相关文章
- python获取当前日期前后N天或N月的日期
# -*- coding: utf-8 -*- '''获取当前日期前后N天或N月的日期''' from time import strftime, localtime from datetime im ...
- python里如何获取当前日期前后N天或N月的日期
#!/usr/bin/python#_*_ coding:UTF-8_*_ import timeimport datetimeimport mathimport calendar ''' time. ...
- JAVA获取当前日期的下周一到下周日的所有日期集合
/** * 获取当前日期的下周一到下周日的所有日期集合 * @return */ public static List getNextWeekDateList(){ Calendar cal1 = C ...
- JAVA获取当前日期所在的周一到周日的所有日期集合
/** * 获取当前日期所在的周一到周日的所有日期集合 * @return */ public static List<Date> getWeekDateList() { Calendar ...
- JAVA获取当前日期指定月份后(多少个月后)的日期
环境要求:使用jdk1.8 package com.date; import java.text.ParseException; import java.text.SimpleDateFormat; ...
- Python中获取当前日期的格式
在Python里如何获取当前的日期和时间呢?在Python语言里,我们可以通过调用什么模块或者类函数来得到当前的时间或日期呢? 当然你可以使用时间模块(time module),该模块提供了各种和时间 ...
- python中获取当前日期在当月是第几天
- JAVA获取当前日期时间所在周的周一和周日日期
/** * 获取当前时间所在周的周一和周日的日期时间 * @return */ public static Map<String,String> getWeekDate() { Map&l ...
- js获取当前日期、前一天、后一天的日期的例子
<script> function addByTransDate(dateParameter, num) { var translateDate = "", dateS ...
随机推荐
- Python语言与其他语言对比
python作为一门高级编程语言,它的诞生虽然很偶然,但是它得到程序员的喜爱却是必然之路,以下是Python与其他编程语言的优缺点对比: 一:简介 1.Python 优势:简单易学,能够把用其他语言制 ...
- 十一、Linux 命令大全
Linux 命令大全 Linux 命令大全 1.文件管理 cat chattr chgrp chmod chown cksum cmp diff diffstat file find git gitv ...
- JavaScript--获取页面盒子中鼠标相对于盒子上、左边框的坐标
分析: 外层边框是浏览器边框,内部盒子是页面的一个盒子,绿点是盒子中鼠标的位置.鼠标相对盒子边框的坐标=页面中(注意不是浏览器)鼠标坐标-盒子相对于浏览器边框的偏移量 第一步:求浏览器边框位置 x=e ...
- vs2017升级、安装
图解VS 2017升级并安装.NET Core 2.1 SDK https://jingyan.baidu.com/album/ff42efa9fb5816c19e2202ef.html?picind ...
- 21.1 XMLHttpRequest 对象【JavaScript高级程序设计第三版】
IE5 是第一款引入XHR 对象的浏览器.在IE5 中,XHR 对象是通过MSXML 库中的一个ActiveX对象实现的.因此,在IE 中可能会遇到三种不同版本的XHR 对象,即MSXML2.XMLH ...
- flask迁移
from flask_script import Managerfrom flask_migrate import Migrate, MigrateCommandfrom info import cr ...
- Python3爬虫(五)解析库的使用之XPath
Infi-chu: http://www.cnblogs.com/Infi-chu/ XPath: 全称是 XML Path Language,XML路径语言,它是一门在XML文档中和HTML文档中查 ...
- java 上溯造型与下塑造型
父类: package com.neusoft.chapter07; public class Father { public int i = 1; public void say(){ System ...
- PHP.37-TP框架商城应用实例-后台13-商品管理-扩展分类的添加、显示【数据分组】、搜索分类【多对多】
商品扩展分类 需求:一件商品能有多个扩展分类,搜索任何一个分类都能搜出该商品 建表[扩展分类表] drop table if exists p39_goods_cat; create table p3 ...
- React实现最完整的百度搜索框
import React,{Component} from 'react' import ReactDOM,{render} from 'react-dom' import 'bootstrap/di ...