14 lines
544 B
JavaScript
14 lines
544 B
JavaScript
export function getDay(num, str) {
|
||
//如果要获取昨天的日期,num就是-1, 前天的就是-2,依次类推。str表示年月日间的分割方式。
|
||
const today = new Date()
|
||
const nowTime = today.getTime()
|
||
const ms = 24 * 3600 * 1000 * num
|
||
today.setTime(nowTime + ms)
|
||
const oYear = today.getFullYear()
|
||
let oMoth = (today.getMonth() + 1).toString()
|
||
if (oMoth.length <= 1) oMoth = "0" + oMoth
|
||
let oDay = today.getDate().toString()
|
||
if (oDay.length <= 1) oDay = "0" + oDay
|
||
return oYear + str + oMoth + str + oDay
|
||
}
|