您的位置:首页 > 产品设计 > UI/UE

Tue Aug 11 2020 11:34:00 GMT+0800 (中国标准时间)进行显示格式化

2020-08-11 16:00 302 查看

把标准时间用new Date声明一下,然后用原生拼接就可以了。
代码如下:

const value = new Date();
getYear = value.getFullYear() + '-' + (value.getMonth() + 1) + '-' + value.getDate() + ' ' + value.getHours() + ':' + value.getMinutes() + ':' + value.getSeconds();

今天做的一个功能虽然简单,但是对于我这个小白来说,搞了半天,需求是默认显示的是
第一次显示的是双向绑定的值,很显然不对,后来显示出来的是Tue Aug 11 2020 15:47:00 GMT+0800 (中国标准时间),很显然也不对,然后我时间格式化以后,需要在刚加载页面的时候就获取,当我在生命周期里面写上方法以后,只能显示一个报错:
Cannot read property ‘$el’ of undefined
应该是因为在刚加载的时候,因为我给他默认先隐藏的,所以就获取不到这个元素,后来脑子灵光了一下,我可以先动态渲染value的值,然后点击事件在获取 ‘el’ 的值就可以了。代码如下

<div>
<input class="current" type="text" :value='timeMonth' @click = 'clickDateMonth'>
<van-datetime-picker
ref='vanDatetimeMonth'
class="vanPicker"
v-model="currentDateYear"
type="datetime"
title="选择完整时间"
:min-date="minDate"
:max-date="maxDate"
@confirm="confirmData"
@cancel='cancelData'
>
</van-datetime-picker>
</div>
// 生命周期
created () {
this.suceuend();
},
methods: {
// 在生命周期刚渲染的时候就让时间格式化,渲染到value的值上,
suceuend () {
let slowDate = new Date();
if (slowDate.getMinutes() <= 9) {
youWant = slowDate.getFullYear() + '-' + (slowDate.getMonth() + 1) + '-' + slowDate.getDate() + ' ' + slowDate.getHours() + ':' + '0' + slowDate.getMinutes();
} else {
youWant = slowDate.getFullYear() + '-' + (slowDate.getMonth() + 1) + '-' + slowDate.getDate() + ' ' + slowDate.getHours() + ':' + slowDate.getMinutes();
}
// 渲染value值
this.timeYear = youWant;
this.timeMonth = youWant;
},
}
// 点击的时候,这个是vant组件自带的事件,点击的时候,默认形参value值,就是当前时间,我在这把时间拼接了一下
confirm (value) {
console.log(value);
// 只是格式化时间的,如果想要以前的年份的话就这传参这儿减value.getFullYear() - 1
if (value.getMinutes() <= 9) {
youWant = value.getFullYear() + '-' + (value.getMonth() + 1) + '-' + value.getDate() + ' ' + value.getHours() + ':' + '0' + value.getMinutes();
} else {
youWant = value.getFullYear() + '-' + (value.getMonth() + 1) + '-' + value.getDate() + ' ' + value.getHours() + ':' + value.getMinutes();
};
this.timeYear = youWant;
// 点击的时候获取就不报错了
this.$refs.vanDatetime.$el.style = 'display: none'
},
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐