-- 创造无限可能

js开发技巧:获取当前年份、月份

2023-10-22 10:56:09
1471 人浏览 0 人点赞
有用,点赞支持一下

场景

获取当前年份、月份

实现

let date = new Date()
let currentYear = date.getFullYear() //获取完整的年份(4位)
let currentMonth = date.getMonth() + 1 //获取当前月份(1-12,1代表1月)
currentMonth = currentMonth >= 10 ? currentMonth : "0" + currentMonth