vue new Date() 获取当前时间
日常工作笔记日常工作笔记 在created添加created: function() {var aData = new Date();this.value =aData.getFullYear() + "-" + (aData.getMonth() + 1) + "-" + aData.getDate();console.log(this.val...
·
日常工作笔记
链接:
http://fjfblog.com
1.new Date() 获取当前时间
created: function() {
var aData = new Date();
console.log(aData) //Wed Aug 21 2019 10:00:58 GMT+0800 (中国标准时间)
this.value =
aData.getFullYear() + "-" + (aData.getMonth() + 1) + "-" + aData.getDate();
console.log(this.value) //2019-8-20
},
补充:
用三元进行单数前面补0
var aData = new Date();
var month =data.getMonth() < 9 ? "0" + (data.getMonth() + 1) : data.getMonth() + 1;
var date = data.getDate() <= 9 ? "0" + data.getDate() : data.getDate();
this.value = data.getFullYear() + "-" + month + "-" + date;
在当前时间加一天:86400000 == 24 * 60 * 60 *1000
int days = 1;var newDate = new Date(Date.now() + days*24*60*60*1000);
在main.js添加倒计时一天删除本地数据:
// 倒计时一天
Vue.prototype.setExpire = (key, value, expire) => {
let obj = {
data: value,
time: Date.now(),
expire: expire
};
localStorage.setItem(key, JSON.stringify(obj));
}
Vue.prototype.getExpire = key => {
let val = localStorage.getItem(key);
if (!val) {
return val;
}
val = JSON.parse(val);
if ((Date.now() - val.time) > val.expire) {
// 放清除的数据
localStorage.removeItem("token");
localStorage.removeItem(key);
router.push({ path: "/" });
return null;
}
return val.data;
}
其他组件可以调用和获取
this.setExpire("token1", "xxxxxxxx", 86400000);
//可以用定时器定时获取当前时间
this.getExpire("token1");
链接:http://fjfblog.com/blog/001577956957334bb65daf2a8034109aa82dd1b76ef8070000
更多推荐
所有评论(0)