[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;
                    })
                },

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值