# 当前季度 now_quarter = now.month / 3 if now.month % 3 == 0 else now.month / 3 + 1 print('--- now_quarter = {}'.format(now_quarter)) # 本周第一天和最后一天 this_week_start = now - timedelta(days=now.weekday()) this_week_end = now + timedelta(days=6 - now.weekday()) print('--- this_week_start = {} this_week_end = {}'.format(this_week_start, this_week_end)) # 上周第一天和最后一天 last_week_start = now - timedelta(days=now.weekday() + 7) last_week_end = now - timedelta(days=now.weekday() + 1) print('--- last_week_start = {} last_week_end = {}'.format(last_week_start, last_week_end)) # 本月第一天和最后一天 this_month_start = datetime.datetime(now.year, now.month, 1) this_month_end = datetime.datetime(now.year, now.month + 1, 1) - timedelta(days=1) print('--- this_month_start = {} this_month_end = {}'.format(this_month_start, this_month_end)) # 上月第一天和最后一天 last_month_end = this_month_start - timedelta(days=1) last_month_start = datetime.datetime(last_month_end.year, last_month_end.month, 1) print('--- last_month_end = {} last_month_start = {}'.format(last_month_end, last_month_start)) # 本季第一天和最后一天 month = (now.month - 1) - (now.month - 1) % 3 + 1 this_quarter_start = datetime.datetime(now.year, month, 1) this_quarter_end = datetime.datetime(now.year, month + 3, 1) - timedelta(days=1) print('--- this_quarter_start = {} this_quarter_end = {}'.format(this_quarter_start, this_quarter_end)) # 上季第一天和最后一天 last_quarter_end = this_quarter_start - timedelta(days=1) last_quarter_start = datetime.datetime(last_quarter_end.year, last_quarter_end.month - 2, 1) print('--- last_quarter_start = {} last_quarter_end = {}'.format(last_quarter_start, last_quarter_end)) # 本年第一天和最后一天 this_year_start = datetime.datetime(now.year, 1, 1) this_year_end = datetime.datetime(now.year + 1, 1, 1) - timedelta(days=1) print('--- this_year_start = {} this_year_end = {}'.format(this_year_start, this_year_end)) # 去年第一天和最后一天 last_year_end = this_year_start - timedelta(days=1) last_year_start = datetime.datetime(last_year_end.year, 1, 1) print('--- last_year_start = {} last_year_end = {}'.format(last_year_start, last_year_end)) import datetimefrom datetime import timedeltanow = datetime.datetime.now()# 今天today = nowprint('--- today = {}'.format(today))# 昨天yesterday = now - timedelta(days=1)print('--- yesterday =... 书写一个函数爬取整个数据库的文件,主要是通过每周的定时爬取 为了凸显每周定时爬取,需要 获取 上周 的时间节点 之前写过类似的时间格式函数: Python 关于strftime函数详细解析 附实战代码 主要的包为:import datetime 1. 基本函数 对于datetime 获取 这些功能函数,需要了解一些基本函数 Date date = new Date();//获得系统时间. SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss"); String nowTime = sdf.format(date); Date time = sdf.parse(nowTime); 2、 获取 昨天时间(年月日时分秒) Date dN. public static String getLastWeekMonday(String time) { SimpleDateFormat foramt = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = foramt.parse(time); } catch (P. export function getThisWeekData() {//获得本周周一~周日的年月日 var thisweek = {}; var date = new Date(); // 本周一的 日期 date.setDate(date.getDate() - date.getDay() + 1); thisweek.... SUBSTR(FROM_UNIXTIME(UNIX_TIMESTAMP()),1,10) ,--out 2021-09-08 执行代码 日期 ,当日 日期 concat(SUBSTR(DATE_SUB(FROM_UNIXTIME(UNIX_TIMESTAMP()),DAY(FROM_UNIXTIME(UNIX_TIMESTAMP()))),1,7) ,'-01'), --out 2021-08-01 上月月初 DATE_SUB(FROM_UNIXTIME(UNIX_TIMESTAMP(. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse("2021-02-02"); Calendar calendar = Calendar.getInstance(); calendar....
package com.goldcn.jzgmanageplat.b2b.controller; import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar; import com.goldcn.common.utils....
# 获取 当月 最后 一天 last_day = first_day.replace(month=first_day.month+1, day=1) - datetime.timedelta(days=1) print("当月 第一天 :", first_day) print("当月 最后 一天 :", last_day) 输出结果如下: 当月 第一天 : 2022-01-01 当月 最后 一天 : 2022-01-31