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

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>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值