效果图:
话不多说,直接看代码:
var date = new Date();
this.year = date.getFullYear();//年
this.month = date.getMonth() + 1;//月
this.strDate = date.getDate();//日
if (this.month >= 1 && this.month <= 9) { this.month = "0" + this.month;}
if (this.strDate >= 0 && this.strDate <= 9) {this.strDate = "0" + this.strDate;}
console.log(this.year+'-'+this.month+'-'+this.strDate)
在mounted中直接定义一个即可,也可以在methods中写一个时间方法,哪里要用直接调用那个方法即可,都行,看个人喜好,
如果还需要时分秒在后面加就可以了,
时:getHours() (0-23)
分:getMinutes() (0-59)
秒:getSeconds() (0-59)
。。。
时分秒都是跟月份一样,从0开始数的,不用+1,因为月是1-12月,而时分秒是0-23和0-59。没了,结束了,是不是很简单呐
最后:如果此篇博文对您有帮助,还请动动小手点点关注点点赞呐~,谢谢 ~ ~