您的位置:首页 > 编程语言 > Python开发

五、python日期相关的操作

2017-08-29 00:00 127 查看
@classmethod
def getMonthRange(cls, year, month):
'''
根据指定年和月,获得该月的第一天和最后一天日期
@param year: 指定的年
@param month: 指定的月
'''
startDate = '%d-%02d-01' % (year, month)
# 得到本月的天数 第一返回为月第一日为星期几(0-6), 第二返回为此月天数
wday, monthRange = calendar.monthrange(year, month)
endDate = '%d-%02d-%02d' % (year, month, monthRange)

return startDate, endDate

使用方式

now = time.localtime()
startDate, endDate = MyUtil.getMonthRange(now.tm_year, now.tm_mon)

获得当前日期之前30天的日期

startDayStr = (datetime.datetime.today() - datetime.timedelta(30)).strftime('%Y-%m-%d')
# 今天的字符串
todayStr = datetime.datetime.now().strftime('%Y-%m-%d')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python