微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[javascript] js判断对象类型typeof与instanceof解决elementui时间插件默认时间问题

页面上有一个时间插件 , 认是没有绑定初始时间的 , 当需要绑定初始时候时 , 只能给它赋值当前日期的Date对象

但是在调用接口的时候 , 要求传递的是一个指定格式的字符串 , 需要把Date对象转成时间字符串 , 这个时候就需要判断类型了

 

typeof 一般只能返回如下几个结果:"number"、"string"、"boolean"、"object"、"function" 和 "undefined"。无法指定是Date类型 , 因此这里需要使用instanceof

用法是console.log(xxx instanceof Date)  这个语句会返回true或者false ,来判断对象类型

 

在elementui下给时间认值这样用 , html部分

<el-date-picker
                                                    v-model="pickTime"
                                                    align="right"
                                                    type="date"
                                                    placeholder="选择日期"
                                                    value-format="yyyyMMdd"
                                                    :picker-options="pickerOptions">
                                            </el-date-picker>

绑定的pickTime , 在data里给一个初始值 pickTime:new Date(),

在进行搜索查询函数里进行类型判断 , 并且转换一下 , 格式转换函数上一篇文章

                getKeywordsList: function () {
                    let _this = this;
                    let data={};
                    if(this.keywordsearchKey!=""){
                        data.word=this.keywordsearchKey;
                    }
                    if(this.pickTime instanceof Date){
                        data.dt=this.formateTime(this.pickTime);
                    }
                    if(this.pickTime != "" && !(this.pickTime instanceof Date)){
                        data.dt=this.pickTime;
                    }
                    $.get('index.PHP?r=media/getkeywordsdata',data, function (rs) {
                        let res= JSON.parse(rs);
                        _this.keywordsList=res.result.rows;
                        _this.keywordCount=res.result.total;
                        _this.keywordsearchCount=res.result.total;
                        _this.displayAll();
                        _this.fullscreenLoading=false;
                    })
                },

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐