五、python日期相关的操作

xiaoxiao2021-02-28  166

@classmethod def getMonthRange(cls, year, month): ''' 根据指定年和月,获得该月的第一天和最后一天日期 @param year: 指定的年 @param month: 指定的月 ''' startDate = '%d-d-01' % (year, month) # 得到本月的天数 第一返回为月第一日为星期几(0-6), 第二返回为此月天数 wday, monthRange = calendar.monthrange(year, month) endDate = '%d-d-d' % (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')
转载请注明原文地址: https://www.6miu.com/read-29714.html

最新回复(0)