vben admin官网: https://doc.vvbin.cn
获取一天24小时内所有[时:分]
// 获取一天内所有[时:分]
const timesOfDay: string[] = [];
function generateTimesOfDay() {
const start = '00:00';
const end = '23:59';
const startTime = new Date(`1970-01-01T${start}`).getTime();
const endTime = new Date(`1970-01-01T${end}`).getTime();
const oneMinute = 60000; // 1 minute in milliseconds
for (let time = startTime; time <= endTime; time += oneMinute) {
const hours = new Date(time).getHours().toString().padStart(2, '0');
const minutes = new Date(time).getMinutes().toString().padStart(2, '0');
timesOfDay.push(`${hours}:${minutes}`);
}
console.log(timesOfDay);
return timesOfDay;
}
获取指定时间段日期
<script setup lang="ts">
import moment from 'moment';
let data: string[] = [];
let start_time = moment().add(-30, 'day').format('YYYY-MM-DD');
let end_time = moment().subtract(1, 'day').format('YYYY-MM-DD');
getRangeDate(start_time, end_time);
// 获取指定时间段日期
function getRangeDate(begin, end) {
let arr1 = begin.split('-');
let arr2 = end.split('-');
let arr1_ = new Date();
arr1_.setUTCFullYear(arr1[0], arr1[1] - 1, arr1[2]);
let arr2_ = new Date();
arr2_.setUTCFullYear(arr2[0], arr2[1] - 1, arr2[2]);
let unixDb = arr1_.getTime();
let unixDe = arr2_.getTime();
for (let k = unixDb; k <= unixDe; k = k + 24 * 60 * 60 * 1000) {
data.push(datetimeparse(k, 'YY-MM-DD'));
}
console.log('data', data);
return data;
}
// 时间格式处理
function datetimeparse(timestamp, format, prefix = '') {
if (typeof timestamp == 'string') {
timestamp = Number(timestamp);
}
//转换时区
let currentZoneTime = new Date(timestamp);
let currentTimestamp = currentZoneTime.getTime();
let offsetZone = currentZoneTime.getTimezoneOffset() / 60; //如果offsetZone>0是西区,西区晚
let offset = 0;
//客户端时间与服务器时间保持一致,固定北京时间东八区。
offset = offsetZone + 8;
currentTimestamp = currentTimestamp + offset * 3600 * 1000;
let newtimestamp = '';
if (currentTimestamp) {
if (currentTimestamp.toString().length === 13) {
newtimestamp = currentTimestamp.toString();
} else if (currentTimestamp.toString().length === 10) {
newtimestamp = currentTimestamp + '000';
} else {
newtimestamp = '';
}
} else {
newtimestamp = '';
}
let dateobj = newtimestamp ? new Date(parseInt(newtimestamp)) : new Date();
let YYYY = dateobj.getFullYear();
let MM = dateobj.getMonth() > 8 ? dateobj.getMonth() + 1 : '0' + (dateobj.getMonth() + 1);
let DD = dateobj.getDate() > 9 ? dateobj.getDate() : '0' + dateobj.getDate();
let HH = dateobj.getHours() > 9 ? dateobj.getHours() : '0' + dateobj.getHours();
let mm = dateobj.getMinutes() > 9 ? dateobj.getMinutes() : '0' + dateobj.getMinutes();
let ss = dateobj.getSeconds() > 9 ? dateobj.getSeconds() : '0' + dateobj.getSeconds();
let output = '';
let separator = '-';
if (format) {
separator = format.match(/-/) ? '-' : '/';
output += format.match(/yy/i) ? YYYY : '';
output += format.match(/MM/) ? (output.length ? separator : '') + MM : '';
output += format.match(/dd/i) ? (output.length ? separator : '') + DD : '';
output += format.match(/hh/i) ? (output.length ? ' ' : '') + HH : '';
output += format.match(/mm/) ? (output.length ? ':' : '') + mm : '';
output += format.match(/ss/i) ? (output.length ? ':' : '') + ss : '';
} else {
output += YYYY + separator + MM + separator + DD;
}
output = prefix ? prefix + output : output;
return newtimestamp ? output : '';
}
</script>
将一个数组分割成多个
function chunkArray<T>(array: T[], chunkSize: number): T[][] {
return array.reduce((chunks: T[][], item: T, index: number) => {
const chunkIndex = Math.floor(index / chunkSize);
if (!chunks[chunkIndex]) {
chunks[chunkIndex] = [];
}
chunks[chunkIndex].push(item);
return chunks;
}, [] as T[][]);
}