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' },
相关文章推荐
- java Tue Aug 21 2018 00:00:00 GMT+0800 (中国标准时间) 00:00:00 时间格式转换
- java Tue Aug 21 2018 00:00:00 GMT+0800 (中国标准时间) 00:00:00 时间格式转换
- js 格式化 extjs 的时间对象 Thu May 12 2016 08:00:00 GMT+0800 (中国标准时间)
- JS格式化Thu May 12 2016 08:00:00 GMT+0800 (中国标准时间)
- JS格式化Thu May 12 2017 08:00:00 GMT+0800 (中国标准时间)
- JS工作随笔 05 JS格式化Thu May 12 2016 08:00:00 GMT+0800 (中国标准时间)
- 将显示到html[object,object]时间进行格式化处理
- Java 如何格式化中国标准时间
- Fri Sep 08 2017 17:03:02 GMT+0800 (中国标准时间)------- 格式的时间转换
- 微信小程序 GMT+0800 (中国标准时间) WXSS 文件编译错误
- JAVA将字符串Sat Nov 25 2017 00:00:00 GMT+0800 (中国标准时间)转成标准时间
- 【Date】日期格式转换 Thu Sep 07 2017 00:00:00 GMT+0800 (中国标准时间) 00:00:00
- 格林时间:Thu May 18 2017 00:00:00 GMT+0800 (中国标准时间) 格式转换
- 微信小程序 GMT+0800 (中国标准时间) WXSS 文件编译错误
- string datdString='Wed Oct 12 2016 00:00:00 GMT+0800 (中国标准时间)'将字符串转化为date类型,格式2016-10-12
- 2017 10:35:13 GMT+0800 (中国标准时间)
- Thu May 12 2016 08:00:00 GMT+0800 (中国标准时间)转化为yyyy-MM-dd(通过js)
- Mon Jun 06 2016 08:00:00 GMT+0800 (中国标准时间)
- 微信小程序 GMT+0800 (中国标准时间) WXSS 文件编译错误
- Ext可编辑表格中timefield选择后会显示中国标准时间