js获取当前年+月格式

我的使用场景  vue3后台项目

需求: 页面初始化时 显示当前年加月格式时间

首先封装  年+月时间公共方法util

//获取年份+月份格式 如'2023-08'
export const getYearMonth = (newDate) => {
  // const currentDate = new Date()
  const year = newDate.getFullYear()
  const month = newDate.getMonth() + 1 // 月份从 0 到 11 表示,需要加 1
  const yearAndMonth = year.toString() + '-' + month.toString().padStart(2, '0')
  return yearAndMonth
}

此时拿到的yearAndMonth为String类型    如 '2023-08'

然后在当前页面   import   引入我们封装好的公共方法

获取当前时间

先定义

//获取当前年+月格式  如'2023-08'
const currentDate = new Date()
const yearMonth = getYearMonth(currentDate)

后取值

const queryParams = reactive({
// subsidyMonth: ['2023-08', '2023-08'],//补贴月份区间
  subsidyMonth: [yearMonth, yearMonth], //补贴月份区间
})

这样就可以成功显示当前 年+月格式的时间了

效果如下

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值