2019-01-08 vue時(shí)間與時(shí)間戳互相轉(zhuǎn)換(iview表單回填驗(yàn)證不通過問題解決)

//獲取時(shí)間
var date = new Date('xxxx-xx-xx xx:xx:xx:xx');


// 日期轉(zhuǎn)時(shí)間戳
    // 有三種方式
    var time1 = date.getTime();
    var time2 = date.valueOf();
    var time3 = Date.parse(date);
以上三種獲取方式的區(qū)別:
第一、第二種:會(huì)精確到毫秒
第三種:只能精確到秒,毫秒用000替代

//時(shí)間戳轉(zhuǎn)日期
 function(fmt) { //
        let o = {
            "M+" : this.getMonth()+1,                 //月份
            "d+" : this.getDate(),                    //日
            "h+" : this.getHours(),                   //小時(shí)
            "m+" : this.getMinutes(),                 //分
            "s+" : this.getSeconds(),                 //秒
            "q+" : Math.floor((this.getMonth()+3)/3), //季度
            "S"  : this.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt))
            fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
        for(var k in o)
            if(new RegExp("("+ k +")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        return fmt;
}

//iview的DatePicker時(shí)間帶T帶Z格式轉(zhuǎn)化時(shí)間戳
changeDate(dateA) {
  var dateee = new Date(dateA).toJSON();
  var date = new Date(+new Date(dateee)+8*3600*1000).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,'');
  return date ;
};

示例:vue 在iview表格的columns寫render轉(zhuǎn)換表格時(shí)間

  data() {
    return {
      columns: [
        {
          title: "任期截止",
          key: "endDate",
          align: "center",
          render: function (h, params) {
            return h('span', new Date(params.row.endDate).Format('yyyy-MM-dd hh:mm:ss'));/*這里的this.row能夠獲取當(dāng)前行的數(shù)據(jù)*/
          }
        },
      ],
    }
  },
  methods: {
    initFormatter(){//時(shí)間戳轉(zhuǎn)換時(shí)間
      Date.prototype.Format = function(fmt) { //
        let o = {  
            "M+" : this.getMonth()+1,                 //月份  
            "d+" : this.getDate(),                    //日  
            "h+" : this.getHours(),                   //小時(shí)  
            "m+" : this.getMinutes(),                 //分  
            "s+" : this.getSeconds(),                 //秒  
            "q+" : Math.floor((this.getMonth()+3)/3), //季度  
            "S"  : this.getMilliseconds()             //毫秒  
        };  
        if(/(y+)/.test(fmt))  
            fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));  
        for(var k in o)  
            if(new RegExp("("+ k +")").test(fmt))  
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));  
        return fmt;  
      }
    },
  },
  created() {
    this.initFormatter();// 為Date 對(duì)象添加Format方法
  },

iview綁定表單日期的時(shí)候(解決回填驗(yàn)證不通過的問題)注意以下問題即可:
如果后端返回給你的對(duì)應(yīng)類型是string,則用:value綁定;
如果后端返回給你的對(duì)應(yīng)類型是date 則用v-model綁定;
并且注意時(shí)間格式的轉(zhuǎn)換(轉(zhuǎn)換為data或者string),
如果表單需要回填,特別注意data上時(shí)間初始值
如果初始值是設(shè)置 ‘’ 空字符串,那么時(shí)間格式應(yīng)該轉(zhuǎn)換為string類型,并且用:value來綁定,且設(shè)置表單規(guī) 則rule的時(shí)候要設(shè)置type:string
如果初始值是設(shè)置 new Date(),那么時(shí)間格式應(yīng)該轉(zhuǎn)換為Date類型,并且用v-mode來綁定,且設(shè)置表單規(guī)則rule的時(shí)候要設(shè)置type:date

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容