在 Vue 中时间戳的转换可以使用 JavaScript 的 Date 对象来实现。下面是一些常见的时间戳转换方法:

时间戳转日期字符串
// 将时间戳转换为日期字符串,例如:2022-01-01
function timestampToDateString(timestamp) {
  const date = new Date(timestamp);
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;
}
<template>
  <div>
    <p>{{ timestampToDateString(timestamp) }}</p>
    <p>{{ timestampToDateTimeString(timestamp) }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      timestamp: 1640995200000,
    };
  },
  methods: {
    timestampToDateString(timestamp) {
      // 调用时间戳转日期字符串方法
      return timestampToDateString(timestamp);
    },
    timestampToDateTimeString(timestamp) {
      // 调用时间戳转日期时间字符串方法
      return timestampToDateTimeString(timestamp);
    },
  },
};
</script>

By lxcss

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注