a-range-picker 时间选择器的默认日期显示,日期格式化

效果图

在这里插入图片描述

代码

<a-range-picker v-model:value="date" show-time />

js代码处理: 前置+0,时间格式化

const handleTime = (t) => {
  return t < 10 ? "0" + t : t;
};
const handleDate = (date) => {
  const year = date?.$d.getFullYear();
  const month = handleTime(date?.$d.getMonth() + 1);
  const day = handleTime(date?.$d.getDate());
  const hours = handleTime(date?.$d.getHours());
  const minutes = handleTime(date?.$d.getMinutes());
  const seconds = handleTime(date?.$d.getSeconds());
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};
const getInitializeDate = () => {
  let date = new Date();
  let year = date.getFullYear();
  let month = handleTime(date.getMonth() + 1);
  let day = handleTime(date.getDate());
  let s = year + "-" + month + "-01" + " 00:00:00";
  let e =
    year +
    "-" +
    month +
    "-" +
    day +
    ` ${handleTime(date.getHours())}:${handleTime(
      date.getMinutes()
    )}:${handleTime(date.getSeconds())}`;
  const times = { start: s, end: e };
  return times;
};
let { start, end } = getInitializeDate();
let date = ref([
  dayjs(start, "YYYY-MM-DD HH:mm:ss"),
  dayjs(end, "YYYY-MM-DD HH:mm:ss"),
]);

若是需要进行传值,可使用如下代码

const handleSearch = async () => {
  try {
    const res = await GetPersonStatistics({
      data: `${handleDate(date.value[0])}-${handleDate(date.value[1])}`,
    });
  	// ... more
  } catch (error) {
    throw new Error(error)
  }
};

你可能感兴趣的:(JavaScript,TypeScript,脚本语言,Vue,项目问题,javascript,前端,开发语言)