`
- 浏览:
27946 次
- 性别:
- 来自:
北京
-
-
#!/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)
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
使用tkinter包实现备忘录的界面实现,内容涵盖增删改查等(相对而言界面较简陋)
Python 数据科学备忘单Python 基础知识.pdf
python文件操作
内容概要: 这段Python代码是一个备忘录管理工具,名为 "MemoManager",它可以帮助用户记录和管理备忘录条目,包括备忘录的内容。 适用人群: 适用于需要记录和管理备忘录的个人用户、学生、职场人士、项目经理以及在...
全面的Python备忘单:Contents 1. Collections: List, Dictionary, Set, Tuple, Range, Enumerate, Iterator, Generator. 2. Types: Type, String, Regular_Exp, Format, Numbers, Combinatorics, Datetime.3. ...
python(tkinter) 桌面备忘贴 备忘录
python 操作docker 命令, docker命令操作,镜像查看,运行,删除,导出,加载等操作...
详细介绍Python中的文件操作,包括文件操作的各种模式分析、文件夹的递归访问、Excel文件的读取和写入等,并通过具体示例演示说明,非常适合高校老师教学和学生复习使用。
关于python文件操作。
Python正则表达式操作指南 Python正则表达式操作指南 Python正则表达式操作指南
python库的中文参考手册,类似于Java的API文档,可以在里面进行搜索查询等
python 文件操作函数 描叙文件操作函数的功能和作用
arrow:更好的 Python 日期时间操作类库
Python数据库操作函数,直接将数据库操作封装为函数,可直接调用。
Python操作常见Office软件.zip - Python 操作 Excel、Word、Pdf 等。Python操作常见Office软件.zip - Python 操作 Excel、Word、Pdf 等。Python操作常见Office软件.zip - Python 操作 Excel、Word、Pdf 等。Python...
150套Python实战操作源码,其中包含了12章节(详见文件目录): 01python核心基础应用(13套) 02python字符串处理(9套) 03python文件操作(9套) 04pythonGUl界面开发(13套) 05python图形图像与多媒体(15套) ...
Python对Excel操作教程.pdfPython对Excel操作教程.pdfPython对Excel操作教程.pdfPython对Excel操作教程.pdfPython对Excel操作教程.pdfPython对Excel操作教程.pdf
python文件夹操作.doc
近年来随着数据科学的出现,Python 的重要性成倍增加,它已成为数据科学和开发的主要编程语言,...每个备忘单都有其优点,但这里列出的备忘单是一些最流行和使用最广泛的 Python 备忘单,可以帮助你完成 Python 学习。
python3写的操作系统基础实验,对于刚开始学习的人有帮助