一、获取今天之前所有近半年的1号和15号的时间,用于折线图的展示
假如今天是2023年12月11号,获取今天之前所有近半年的1号和15号的时间,即:2023-12-01,2023-11-15,2023-11-01…
最直接的思路,获取到当前时间,去遍历今年以及去年的时间,考虑到如果时间早于6月15日时,今年满足条件的1号以及15号不及12个,以此取前一年满足条件的时间,而今年晚于6月15日后(包括6月15日),获取到今年的所有满足条件的时间后,截取数组即可。
<script>
// 当前日期
const nowDate = new Date(2023, 4, 15, 0, 0, 0);
// 当前年份
const thisYear = nowDate.getFullYear();
// 当前月份
const month = nowDate.getMonth() + 1;
// 当前日
const day = nowDate.getDate() + 1;
// 当前年所有1号及15号(不包括超过当前日的)
let thisYearDate = [];
// 去年
let lastYearDate = [];
// 目标日期数组
let yearDate = []
for (let i = 0; i < month; i++) {
const date = new Date(thisYear, i, 1);
if (date <= nowDate) {
date.setDate(2);
thisYearDate.push(date.toISOString().slice(0, 10))
}
const date15 = new Date(thisYear, i, 15);
if (date15 <= nowDate) {
date15.setDate(16);
thisYearDate.push(date15.toISOString().slice(0, 10))
}
}
if (thisYearDate.length < 12) {
for (let i = 0; i < 12; i++) {
const date = new Date(thisYear - 1, i, 1);
if (date <= nowDate) {
date.setDate(2);
lastYearDate.push(date.toISOString().slice(0, 10))
}
const date15 = new Date(thisYear - 1, i, 15);
if (date15 <= nowDate) {
date15.setDate(16);
lastYearDate.push(date15.toISOString().slice(0, 10))
}
}
yearDate = [...lastYearDate.slice(lastYearDate.length - (12 - thisYearDate.length), lastYearDate.length), ...thisYearDate]
} else {
yearDate = thisYearDate.slice(thisYearDate.length - 12, thisYearDate.length)
}
console.log(yearDate)
// ['2023-06-15', '2023-07-01', '2023-07-15', '2023-08-01', '2023-08-15', '2023-09-01', '2023-09-15', '2023-10-01', '2023-10-15', '2023-11-01', '2023-11-15', '2023-12-01']
</script>
为什么const month = now.getMonth() + 1;
这里的当前月份需要加一?
因为 JavaScript 中的 Date 对象中的月份是从 0 开始计数的,即 0 表示一月,1 表示二月,以此类推,11 表示十二月。
date.toISOString()
的作用?
不使用这个方法的话,直接打印new Date(),结果是Mon Dec 11 2023 15:42:28 GMT+0800 (中国标准时间)
;使用toISOString() 方法返回一个符合 ISO 8601 标准的字符串表示日期和时间的格式,它将日期和时间转换为 UTC 时间,并以以下格式返回字符串:YYYY-MM-DDTHH:mm:ss.sssZ
,此时我们得到结果'2023-12-11T07:44:12.173Z'
,后截取字符串,得到YYYY-MM-DD
格式的时间。
二、那假定是柱状图呢?
柱状图往往是一个区间,因此还需对以上代码及数据进行一定的处理(此前获得的12个数据显然是不够,因此还需多取一个,此处我们依旧以取前不去后的原则去取)。
<script>
// 当前日期
const nowDate = new Date(2023, 11, 15, 0, 0, 0);
// 当前年份
const thisYear = nowDate.getFullYear();
// 当前月份
const month = nowDate.getMonth() + 1;
// 当前日
const day = nowDate.getDate() + 1;
// 当前年所有1号及15号(不包括超过当前日的)
let thisYearDate = [];
// 去年
let lastYearDate = [];
// 目标日期数组
let yearDate = []
for (let i = 0; i < month; i++) {
const date = new Date(thisYear, i, 1);
if (date <= nowDate) {
date.setDate(2);
thisYearDate.push(date.toISOString().slice(0, 10))
}
const date15 = new Date(thisYear, i, 15);
if (date15 <= nowDate) {
date15.setDate(16);
thisYearDate.push(date15.toISOString().slice(0, 10))
}
}
if (thisYearDate.length < 13) {
for (let i = 0; i < 12; i++) {
const date = new Date(thisYear - 1, i, 1);
if (date <= nowDate) {
date.setDate(2);
lastYearDate.push(date.toISOString().slice(0, 10))
}
const date15 = new Date(thisYear - 1, i, 15);
if (date15 <=
nowDate) {
date15.setDate(16);
lastYearDate.push(date15.toISOString().slice(0, 10))
}
}
yearDate = [...lastYearDate.slice(lastYearDate.length - (13 - thisYearDate.length), lastYearDate.length), ...
thisYearDate
]
} else {
yearDate = thisYearDate.slice(thisYearDate.length - 13, thisYearDate.length)
}
console.log(yearDate)
// ['2023-06-15', '2023-07-01', '2023-07-15', '2023-08-01', '2023-08-15', '2023-09-01', '2023-09-15', '2023-10-01','2023-10-15', '2023-11-01', '2023-11-15', '2023-12-01', '2023-12-15']
const formattedDates = yearDate.map((date, index) => {
if (index < yearDate.length - 1) {
return `${date}——${yearDate[index + 1]}`;
}
return date;
});
console.log(formattedDates);
// ['2023-06-15——2023-07-01', '2023-07-01——2023-07-15', '2023-07-15——2023-08-01', '2023-08-01——2023-08-15','2023-08-15——2023-09-01', '2023-09-01——2023-09-15', '2023-09-15——2023-10-01', '2023-10-01——2023-10-15','2023-10-15——2023-11-01', '2023-11-01——2023-11-15', '2023-11-15——2023-12-01', '2023-12-01——2023-12-15', '2023-12-15']
// 去掉最后一个不符合格式要求的,即'2023-12-15',方法处理下来,这是一个单独的
formattedDates.pop()
console.log(formattedDates);
// ['2023-06-15——2023-07-01', '2023-07-01——2023-07-15', '2023-07-15——2023-08-01', '2023-08-01——2023-08-15','2023-08-15——2023-09-01', '2023-09-01——2023-09-15', '2023-09-15——2023-10-01', '2023-10-01——2023-10-15','2023-10-15——2023-11-01', '2023-11-01——2023-11-15', '2023-11-15——2023-12-01', '2023-12-01——2023-12-15']
</script>
三、优化上述代码
可对相关重复的代码进行方法的封装;采用Moment.js、Day.js等第三方库,更便于我们数据的处理。