vue编写组件 根据指定日期获取一周内所有 日期与农历日期展示 并标记当天

这篇具有很好参考价值的文章主要介绍了vue编写组件 根据指定日期获取一周内所有 日期与农历日期展示 并标记当天。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

首先 我们要引入一下对应的第三方依赖

npm install --save chinese-lunar-calendar sass sass-loader

这里 我们需要 chinese-lunar-calendar 将日期变成农历日期的工具
sass是因为 我这里为了方便 用了 sass写样式

组件代码如下

<template>
    <header
      class = "skeleton"
    >
        <div
          class = "day"
          v-for = "item in weeklySet"
          :key = "item.id"
          v-bind:class="{currentDate:item.id == currentWeek}"
        >
            <div class = "top">{{ item.label }}</div>
            <div
              class = "center"
            >{{ item.date }}</div>
            <div>{{ item.calendar }}</div>
        </div>
    </header>
</template>

<script>
import { getLunar } from 'chinese-lunar-calendar'
export default {
    data() {
        return {
            monthComparison: {
                1: 31,
                2: null,
                3: 31,
                4: 30,
                5: 31,
                6: 30,
                7: 31,
                8: 31,
                9: 30,
                10: 31,
                11: 30,
                12: 31
            },
            weeklySet: [
                {
                    id: 0,
                    date: null,
                    label: "周日",
                    calendar: ""
                },
                {
                    id: 1,
                    date: null,
                    label: "周一",
                    calendar: ""
                },
                {
                    id: 2,
                    date: null,
                    label: "周二",
                    calendar: ""
                },
                {
                    id: 3,
                    date: null,
                    label: "周三",
                    calendar: ""
                },
                {
                    id: 4,
                    date: null,
                    label: "周四",
                    calendar: ""
                },
                {
                    id: 5,
                    date: null,
                    label: "周五",
                    calendar: ""
                },
                {
                    id: 6,
                    date: null,
                    label: "周六",
                    calendar: ""
                }
            ],

        }
    },
    methods: {
        enlargeDate(item ,currentWeek , agencyYear, currentMonth, referenceDate) {
            let index = item.id;
            let pendingDate = referenceDate;
            let Month = currentMonth;
            let year = agencyYear;
            while(currentWeek > index){
                if((pendingDate - 1) < 1) {
                    if(Month == 1) {
                        year -= 1;
                        Month = 12;
                        pendingDate = 31;
                    }else{
                        Month -= 1;
                        pendingDate = this.monthComparison[Month];
                    }
                }else{
                    pendingDate -= 1;
                }
                index ++;
            }
            item.date = pendingDate;
            item.calendar = this.getLunar(year,Month,pendingDate);
            return item
        },
        shrinkDate(item ,currentWeek , agencyYear, currentMonth, referenceDate) {
            let index = item.id;
            let pendingDate = referenceDate;
            let Month = currentMonth;
            let year = agencyYear;
            while(currentWeek < index){
                if((pendingDate + 1) > this.monthComparison[Month]) {
                    if(Month == 12) {
                        year += 1;
                        Month = 1;
                        pendingDate = 1;
                    }else{
                        Month += 1;
                        pendingDate = 1;
                    }
                }else{
                    pendingDate += 1;
                }
                index --;
            }
            item.date = pendingDate;
            item.calendar = this.getLunar(year,Month,pendingDate);
            return item
        },
        getLunar(agencyYear,currentMonth,referenceDate) {
            let data = getLunar(agencyYear, currentMonth, referenceDate);
            return data.dateStr.split('月')[1]?data.dateStr.split('月')[1]:data.dateStr.split('月')[0]
        },
        ConversionDate(date) {
            const agencyYear = date.getFullYear();
            const referenceDate = date.getDate();
            const currentWeek = date.getDay();
            const currentMonth = date.getMonth() + 1;
            this.weeklySet = this.weeklySet.map(item => {
                if(item.id > currentWeek) {
                    item = this.shrinkDate(item ,currentWeek , agencyYear, currentMonth, referenceDate);
                }
                if(item.id < currentWeek) {
                    item = this.enlargeDate(item ,currentWeek , agencyYear, currentMonth, referenceDate);
                }
                return item
            })
            console.log(this.weeklySet);
        },
        getAweekago(condition){
            const date = condition?new Date(condition):new Date();
            const agencyYear = date.getFullYear();
            const referenceDate = date.getDate();
            const currentWeek = date.getDay();
            const currentMonth = date.getMonth() + 1;
            this.monthComparison[2] = this.backDay(agencyYear);
            this.weeklySet[currentWeek].date = referenceDate;
            this.weeklySet[currentWeek].calendar = this.getLunar(agencyYear,currentMonth,referenceDate);
            this.currentWeek = currentWeek;
            this.ConversionDate(date);
        },
        backDay(year) {
            return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)?29:28;
        }
    },
    created() {
        this.getAweekago();
    }
}
</script>

<style lang='scss' scoped>
.skeleton {
    padding-left: 76px;
    width: calc(100% - 76px);
    height: 112px;
    display: flex;
    .day{
        flex: 1;
        display: flex;
        flex-direction:column;
        font-size: 14px;
        div{
            display: flex;
            justify-content: center;
        }
        .top{
            padding-top: 12px;
        }
        .center{
            height: 28px;
            width: 28px;
            display: flex;
            justify-content: center;
            align-items: center;
            margin: 6px auto;
            border-radius: 50%;
        }
    }.currentDate{
        color: #21B1FF;
        .center{
            color: #FFFFFF;
            background: #21B1FF;
        }
    }
}
</style>

感兴趣的朋友可以拿出做个二开什么的
效果如下
vue编写组件 根据指定日期获取一周内所有 日期与农历日期展示 并标记当天

跨年跨月这些我都是写了判断的
例如 我们将created 代码修改如下

created() {
    this.getAweekago("2020-12-29");
}

vue编写组件 根据指定日期获取一周内所有 日期与农历日期展示 并标记当天
改为 2020-01-01
vue编写组件 根据指定日期获取一周内所有 日期与农历日期展示 并标记当天
大家都可以自己那个来玩一玩 自认为还是可以去做一个拓展的文章来源地址https://www.toymoban.com/news/detail-449572.html

到了这里,关于vue编写组件 根据指定日期获取一周内所有 日期与农历日期展示 并标记当天的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 使用 Laf 一周内上线美术狮 AI 绘画小程序

    “美术狮 AI 绘画”(以下简称“美术狮”),是我们小团队的一次尝试,定位是人人都可以上手的,充满创意的,理解中文和中国文化的图片生成工具。 在完善图像模型和论证核心问题之后,我们开始构建 MVP(最小化可行产品)。MVP 的构建需要: 实现快,开发周期较短 模

    2024年02月10日
    浏览(23)
  • Java获取指定日期所在周的周一和周日的日期

    原来开发里很少有时间相关的开发任务,Calendar日历类用的太少,随手写的记录一下。 需求:判断用户当周是否已经提交过数据,按周一至周日为一周期来算 参考:Java最准确的获取当前一周开始时间和结束时间

    2024年02月11日
    浏览(76)
  • java获取指定日期是第几周

    需求场景: 根据指定的日期,比如 2023-03-07 ,是第几周 代码如下: 运行结果 2023年3月第2周

    2024年02月16日
    浏览(28)
  • Java获取指定时间一周至周日的日期

     Java获取指定时间一周至周日的日期: 测试类: 输出:  

    2024年02月13日
    浏览(38)
  • Java 获取日期前一年、月、日,指定日期的前几天,后几天

    LocalDate转化为指定格式的字符串 方法1 方法2

    2024年02月12日
    浏览(35)
  • java 获取指定日期的年、月、日、时、分、秒

    1、java 中使用 java.util.Date 获取指定日期的年 2、java 中使用 java.util.Date 获取指定日期的月 3、java 中使用 java.util.Date 获取指定日期的日 4、java 中使用 java.util.Date 获取指定日期的时 5、java 中使用 java.util.Date 获取指定日期的分 6、java 中使用 java.util.Date 获取指定日期的秒 内容如

    2024年02月04日
    浏览(27)
  • vue+element UI中给指定日期添加标记

    1.日期控件中添加:picker-options属性,即:picker-options=“myPickerOptions” 2.在data中定义要标记的日期数组hasXtdsDate,及myPickerOptions处理逻辑,筛选出要标记的日期数组 3.对要进行标记的日期进行数据筛选 4.自定义日期标记的样式 效果如下图:

    2024年03月24日
    浏览(28)
  • java根据当前时间或指定时间获取前后几天或前后几个月或前后几年的时间

    获取前后几天 获取前后几个月 获取前后几年 获取前后几天 获取前后几个月 获取年后几年

    2024年02月15日
    浏览(35)
  • element-ui实现日期选择器最近一周,上一周,下一周功能

    界面部分代码: js部分:

    2024年02月11日
    浏览(31)
  • js怎么计算当前一周的日期

    你可以使用 JavaScript 的 Date 对象来计算当前一周的日期。首先,你需要获取当前日期,然后使用 Date 对象的 getDay 方法获取当前是星期几(星期日是 0,星期一是 1,以此类推)。然后,你可以根据当前是星期几来计算出本周的第一天和最后一天的日期。 例如,下面是一个示例函

    2024年02月09日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包