Vue获取当前日期时间、获取指定时间的前、后多少天的日期
https://blog.csdn.net/ForeverBana/article/details/103472751Vue获取当前日期https://blog.csdn.net/weixin_34301132/article/details/91423700?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMa
·
https://blog.csdn.net/ForeverBana/article/details/103472751 Vue获取当前日期
Vue 获取当前日期 年月日时分秒
2021-7-30 11:17:07
createTime: this.getdateTime(),
getdateTime() {
var _this = this;
let yy = new Date().getFullYear();
let mm = new Date().getMonth() + 1;
let dd = new Date().getDate();
let hh = new Date().getHours();
let mf =
new Date().getMinutes() < 10
? "0" + new Date().getMinutes()
: new Date().getMinutes();
let ss =
new Date().getSeconds() < 10
? "0" + new Date().getSeconds()
: new Date().getSeconds();
let gettime = yy + "-" + mm + "-" + dd + " " + hh + ":" + mf + ":" + ss;
return gettime;
},
定义方法(Vue获取当前日期)
效果
method:{
getdate() {
var date = new Date();
var seperator1 = "-";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = year + " 年 " + month + " 月 " + strDate + " 日 ";
return currentdate;
}
}
调用方法
{{ getdate() }}
https://www.cnblogs.com/aoqizhonghua/p/11775982.html js、vue获取指定时间的前、后多少天的日期
// 获取当前日期前后多少天的日期,多少天前传正数,多少天后传负数,今天传0,
// num为传入的数字, time为传入的指定日期,如果time不传,则默认为当前时间
getBeforeDate(num, time) {
let n = num;
let d = '';
if(time) {
d = new Date(time);
} else {
d = new Date();
}
let year = d.getFullYear();
let mon = d.getMonth() + 1;
let day = d.getDate();
if(day <= n) {
if(mon > 1) {
mon = mon - 1;
} else {
year = year - 1;
mon = 12;
}
}
d.setDate(d.getDate() - n);
year = d.getFullYear();
mon = d.getMonth() + 1;
day = d.getDate();
let s = year + "-" + (mon < 10 ? ('0' + mon) : mon) + "-" + (day < 10 ? ('0' + day) : day);
return s;
}
<template>
<div>
<h3>{{time | formatDate}}</h3>
</div>
</template>
<script>
export default{
data(){
return{
time:Date.parse(new Date()),
},
filters: {
formatDate: function (value) {
let date = new Date(value);
let y = date.getFullYear();
let MM = date.getMonth() + 1;
MM = MM < 10 ? ('0' + MM) : MM;
let d = date.getDate();
d = d < 10 ? ('0' + d) : d;
let h = date.getHours();
h = h < 10 ? ('0' + h) : h;
let m = date.getMinutes();
m = m < 10 ? ('0' + m) : m;
let s = date.getSeconds();
s = s < 10 ? ('0' + s) : s;
return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
}
}
}
}
</script>
更多推荐
已为社区贡献60条内容
所有评论(0)