python获取指定日期的前n天或后n天日期

<pre name="code" class="plain">

首先导入time,和datetime包

import datetime
import time

实现代码如下:

if __name__ == '__main__':
        dt=sys.argv[1]
        myday = datetime.datetime( int(dt[0:4]),int(dt[5:7]),int(dt[8:10]) ) + datetime.timedelta(days=-1)
        dt = myday.strftime('%Y-%m-%d')
        timestamp = int(time.mktime(time.strptime(dt, "%Y-%m-%d")))
        print dt
        datetime.timedelta()
        now_time = datetime.datetime.now()
        yes_time = now_time + datetime.timedelta(days=-2)
        # yes_time_nyr = last_time.strftime('%Y%m%d')
        print now_time
        print yes_time
        print yes_time.strftime('%Y-%m-%d')

运行命令: python test.py 2015-04-01

输出结果:

2015-03-31

2015-04-01 10:46:28.099195

2015-03-30 10:46:28.099195

2015-03-30

时间: 2024-08-01 10:08:57

python获取指定日期的前n天或后n天日期的相关文章

python 获取指定文件列表

glob模块是最简单的模块之一,内容非常少.用它可以查找符合特定规则的文件路径名.跟使用windows下的文件搜索差不多.查找文件只用到三个匹配符:"*", "?", "[]"."*"匹配0个或多个字符:"?"匹配单个字符:"[]"匹配指定范围内的字符,如:[0-9]匹配数字. glob.glob 返回所有匹配的文件路径列表.它只有一个参数pathname,定义了文件路径匹配规则,这里可

js获取指定时间的前几秒

//指定时间减2秒 function reduceTwoS(dateStr){//dateStr格式为yyyy-mm-dd hh:mm:ss var dt=new Date(dateStr.replace(/-/,"/"));//将传入的日期格式的字符串转换为date对象 兼容ie // var dt=new Date(dateStr);//将传入的日期格式的字符串转换为date对象 非ie var ndt=new Date(dt.getTime()-2000);//将转换之后的时间减

Python:爬虫之利用Python获取指定网址上的所有图片—Jaosn niu

# coding=gbk import urllib.request import re import os import urllib def getHtml(url): #指定网址获取函数 page = urllib.request.urlopen(url) html = page.read() return html.decode('UTF-8') def getImg(html): #定义获取图片函数 reg = r'src="(.+?\.jpg)" pic_ext' imgr

python获取指定目录下的所有指定后缀的文件名

使用到的函数有: os.path.splitext():分离文件名与扩展名 os.path.splitext(file)[0] 获得文件名 os.path.splitext(file)[1] 获得文件扩展名

python获取指定网页上的有多少个超级链接

# -*- coding: cp936 -*- import urllib2 import re def u(url): #connect to a URL website = urllib2.urlopen(url) #read html code html = website.read() #use re.findall to get all the links links = re.findall('"((http|ftp)s?://.*?)"', html) print u'有

Python获取指定文件夹下的文件名

os.walk()和os.listdir()两种方法 一.os.walk() 模块os中的walk()函数可以遍历文件夹下所有的文件. os.walk(top, topdown=Ture, onerror=None, followlinks=False) 该函数可以得到一个三元tupple(dirpath, dirnames, filenames). dirpath:string,代表目录的路径: dirnames:list,包含了当前dirpath路径下所有的子目录名字(不包含目录路径): f

Python获取目录、文件的注意事项

Python获取指定路径下的子目录和文件有两种方法: os.listdir(dir)和os.walk(dir),前者列出dir目录下的所有直接子目录和文件的名称(均不包含完整路径),如 >>> os.listdir(r'E:')['$RECYCLE.BIN', 'BaiduYunDownload', 'cabspottingdata', 'cabspottingdata.tar.gz', 'data', 'MyDownloads', 'System Volume Information'

python 获取前一天或前N天的日期

简单实现 import datetime # 获取前1天或N天的日期,beforeOfDay=1:前1天:beforeOfDay=N:前N天 def getdate(self,beforeOfDay): today = datetime.datetime.now() # 计算偏移量 offset = datetime.timedelta(days=-beforeOfDay) # 获取想要的日期的时间 re_date = (today + offset).strftime('%Y-%m-%d')

php获取指定日期的前一天,前一月,前一年日期

前一天的日期为: date("Y-m-d",strtotime("-1 days",strtotime('2019-08-31'))) 前一月的日期为 date("Y-m-d",strtotime("-1 months",strtotime('2019-08-31'))) 前一年的日期为: date("Y-m-d",strtotime("-1 years",strtotime('2019