vue3 获取当前日期的农历(阴历)

第一步,安装 chinese-lunar-calendar 插件

npm install --save chinese-lunar-calendar

第二步,在所需页面 引入

<template>
  <div>
    <p>{{ lunarDay }}</p>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import { getLunar } from "chinese-lunar-calendar";

const lunarDay = ref(""); // 农历日期

onMounted(() => {
  // 初始化日历
  const date = new Date();
  const curYear = date.getFullYear(); // 当前年份
  const curMonth = date.getMonth() + 1; // 当前月份
  const curDay = date.getDate(); // 当前日期

  const lunarDate = getLunar(curYear, curMonth, curDay);
  lunarDay.value = lunarDate.dateStr;
});
</script>

上面代码即可实现所需功能
下面的图片是ui图的样式
在这里插入图片描述
代码如下

<template>
  <div class="fix-box">
    <!-- 日历卡片 页面 -->
    <div class="calendar-card">
      <div class="cal-top">{{ curYear }}{{ curMonth }}</div>
      <div class="cal-center">{{ curDay }}</div>
      <div class="cal-footer">
        <p class="cal-footer-d">{{ dayOfYear }} 天 第 {{ weekOfYear }}</p>
        <p class="cal-footer-w">{{ lunarDay }} {{ curWeek }}</p>
      </div>
    </div>
    <p class="label d-elip">日历</p>
  </div>
</template>
<script setup>
import { onMounted, ref } from "vue";
import { getLunar } from "chinese-lunar-calendar";

const curYear = ref(""); // 当前年份
const curMonth = ref(""); // 当前月份
const curDay = ref(""); // 当前日期
const curWeek = ref(""); // 当前周
const lunarDay = ref(""); // 农历日期
const dayOfYear = ref(""); // 当前日期是一年中的第几天
const weekOfYear = ref(""); // 当前日期是一年的第几周

onMounted(() => {
  // 初始化日历
  const date = new Date();
  curYear.value = date.getFullYear();
  curMonth.value = date.getMonth() + 1;
  curDay.value = date.getDate();

  const weekday = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
  const day = date.getDay();
  curWeek.value = weekday[day];

  const lunarDate = getLunar(curYear.value, curMonth.value, curDay.value);
  lunarDay.value = lunarDate.dateStr;
  // 获取当前时间是当年的第几天
  dayOfYear.value = Math.floor((date - new Date(curYear.value, 0, 0)) / 86400000);
  // 获取当前时间是当年的第几周
  const start = new Date(curYear.value, 0, 1);
  weekOfYear.value = Math.ceil((((date - start) / 86400000) + start.getDay() + 1) / 7);
});
</script>

<style scoped lang="less">
.fix-box {
  position: relative;

  .calendar-card {
    //   width: 100%;
    //   height: 100%;
    width: 186px;
    height: 186px;
    font-family: PingFang SC, -apple-system, BlinkMacSystemFont, Helvetica Neue,
      Helvetica, sans-serif;
    background: #fff;
    border-radius: 15px;
    overflow: hidden;
    transition: transform 0.3s ease;

    &:hover {
      transform: translateY(-3px);
    }

    .cal-top {
      font-size: 16px;
      color: #fff;
      line-height: 46px;
      height: 25%;
      background: rgba(239, 102, 99, 0.8);
    }
    .cal-center {
      font-size: 46px;
      font-weight: 600;
      line-height: 78px;
      height: 42%;
    }
    .cal-footer {
      height: 33%;

      .cal-footer-d {
        font-size: 14px;
        color: #999;
      }
      .cal-footer-w {
        font-size: 16px;
      }
    }
  }
  .label {
    position: absolute;
    bottom: -28px;
    left: 0;
    width: 100%;
    font-size: 14px;
    color: #fff;
  }
  .d-elip {
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
  }
}
</style>
Logo

前往低代码交流专区

更多推荐