有些需求场景需要获取到今日的年月日,于是封装成函数便于调用。
let year = new Date().getFullYear();
let month =
new Date().getMonth() + 1 < 10
? "0" + (new Date().getMonth() + 1)
: new Date().getMonth() + 1;
let date =
new Date().getDate() < 10
? "0" + new Date().getDate()
: new Date().getDate();
let timeFormate = {
//获取年月日
getFull: function () {
return year + "-" + month + "-" + date;
},
//获取年
getY: function () {
return year + "";
},
//获取年月
getM: function () {
return year + "-" + month;
}
};
export default timeFormate;
- 引用
<script>
import timeFormate from "@/tools/timeFormate";
export default {
created() {
timeFormate.getFull();//获取年月日
timeFormate.getY();//获取年
timeFormate.getM();//获取年月
}
};
</script>