前端年份的判断

    /**
     * 是否闰年函数
     * year: Number 当前年份
     */
    isLeap(year) {
      return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
    },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Vue中,我们可以使用Date对象和一些内置的方法来判断昨天是否在当前日期之前。 首先,我们可以通过实例化一个Date对象来获取当前日期: ```javascript const currentDate = new Date(); ``` 然后,我们可以使用`getFullYear()`方法获取当前年份,`getMonth()`方法获取当前月份(注意,这里的月份范围是从0开始的,所以需要加1),`getDate()`方法获取当前日期: ```javascript const currentYear = currentDate.getFullYear(); const currentMonth = currentDate.getMonth() + 1; const currentDay = currentDate.getDate(); ``` 接下来,我们可以创建一个新的Date对象,表示昨天的日期。我们可以使用`setDate()`方法设置当前日期减去1: ```javascript const yesterdayDate = new Date(); yesterdayDate.setDate(currentDay - 1); ``` 最后,我们可以再次使用`getFullYear()`、`getMonth()`和`getDate()`方法获取昨天日期的年份、月份和日期。然后,我们可以通过比较这些值来判断昨天是否在当前日期之前: ```javascript const yesterdayYear = yesterdayDate.getFullYear(); const yesterdayMonth = yesterdayDate.getMonth() + 1; const yesterdayDay = yesterdayDate.getDate(); if (yesterdayYear < currentYear || (yesterdayYear === currentYear && yesterdayMonth < currentMonth) || (yesterdayYear === currentYear && yesterdayMonth === currentMonth && yesterdayDay < currentDay)) { console.log("昨天在当前日期之前"); } else { console.log("昨天不在当前日期之前"); } ``` 这就是在Vue中判断昨天是否在当前日期之前的方法。注意,由于Vue是一个前端框架,以上的代码可以在Vue的组件中使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值