代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Python
】
python获取当前日期前后N天或N月的日期
作者:
/ 发布于
2011/1/4
/
642
<div>!/usr/bin/python ''' Filename: "utildate.py" author: "zhangsong" date : "2009-03-24" version: "1.00" ''' 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 getdayofday(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 getdaysofmonth(year,mon): ''' get days of month ''' return calendar.monthrange(year, mon)[1] def getfirstdayofmonth(year,mon): ''' get the first day of month date format = "YYYY-MM-DD" ''' days="01" if(int(mon)<10): mon = "0"+str(int(mon)) arr = (year,mon,days) return "-".join("%s" %i for i in arr) def getlastdayofmonth(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 = "01" 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 get_today_month(n=0): ''' get last or next month's today n is how many months 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) 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(getdaysofmonth(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(getdaysofmonth(thisyear,j)) j = addzero(j) return (str(thisyear),str(j),days) else: if((totalmon>0) and (totalmon<12)): days = str(getdaysofmonth(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(getdaysofmonth(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 "0"+str(nabs) else: return nabs #print today() #print addzero(10) print get_today_month(-1) print get_lastday_month(3) print get_firstday_month(3) </div>
试试其它关键字
日期
同语言下
.
比较两个图片的相似度
.
过urllib2获取带有中文参数的url内容
.
不下载获取远程图片的宽度和高度及文件大小
.
通过qrcode库生成二维码
.
通过httplib发送GET和POST请求
.
Django下解决小文件下载
.
遍历windows的所有窗口并输出窗口标题
.
根据窗口标题调用窗口
.
python 抓取搜狗指定公众号
.
pandas读取指定列
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3