JS程式碼段:VUE下的時間,星期和年月日
阿新 • • 發佈:2019-01-03
不為別的,只為以後複製貼上方便
data() { return { date: "", time: "", week: "" }; },
//vm.timeOffset是與伺服器時間的偏移量,不用的話自行刪掉
methods: { //獲取當前時間 getNowTime: function() { let vm = this; var nowLocalTime = Date.parse(new Date()); var nowTime = new Date(nowLocalTime + vm.timeOffset);var year = nowTime.getFullYear(); var mounth = nowTime.getMonth() + 1 < 10 ? "0" + (nowTime.getMonth() + 1) : nowTime.getMonth() + 1; var date = nowTime.getDate() < 10 ? "0" + nowTime.getDate() : nowTime.getDate(); vm.date = year + "-" + mounth + "-" + date;var hour = nowTime.getHours() < 10 ? "0" + nowTime.getHours() : nowTime.getHours(); var minute = nowTime.getMinutes() < 10 ? "0" + nowTime.getMinutes() : nowTime.getMinutes(); vm.time = hour + ":" + minute; if (nowTime.getDay() == 0) { vm.week= "星期天"; } else if (nowTime.getDay() == 1) { vm.week = "星期一"; } else if (nowTime.getDay() == 2) { vm.week = "星期二"; } else if (nowTime.getDay() == 3) { vm.week = "星期三"; } else if (nowTime.getDay() == 4) { vm.week = "星期四"; } else if (nowTime.getDay() == 5) { vm.week = "星期五"; } else if (nowTime.getDay() == 6) { vm.week = "星期六"; } } },
mounted: function() { let vm = this; vm.getNowTime(); vm.currentTimeInterval = setInterval(vm.getNowTime(), 1000); }