直接上代码
let y = date.getFullYear()
let m = date.getMonth() - 1 < 10 ? '0' + date.getMonth() + 1 : date.getMonth() + 1
let d = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
const tools = {}
/**
* 日期获取(今天,上一年、下一年)
* @returns {string}
*/
tools.getTodayTime = function () {
return y + '-' + m + '-' + d
}
tools.getLastYearTime = function () {
return y - 1 + '-' + m + '-' + ((parseInt(d) + 1) < 10 ? '0' + (parseInt(d) + 1) : (parseInt(d) + 1))
}
tools.getNextYearTime = function () {
return y + 1 + '-' + m + '-' + ((parseInt(d) - 1) < 10 ? '0' + (parseInt(d) - 1) : (parseInt(d) - 1))
}
这样使用的时候直接:tools.getTodayTime()
如果在vue中注册全局就需要用install方法导出上边的方法
/**
* 导出
* @returns {string}
*/
export default {
install (Vue) {
Vue.prototype.$tools = tools
}
}
然后在mine.js 中
improt tools from './tools.js'
Vue.use(tools)