js最简洁的时间对象转成时间字符串的方法

getTimestr(val){
               let temp = val.toLocaleString()
               if(temp.match(/[\u4e00-\u9fa5]/g)[0]=="上"){
                    temp = temp.replace(/[\u4e00-\u9fa5]/g,"")
                    return temp.slice(-8,-7)==' '? temp.slice(0,-7)+'0'+temp.slice(-7):temp
               }else{
                   temp = temp.replace(/[\u4e00-\u9fa5]/g,"")
                   let templist = temp.split(' ')
                   let temptime = templist[1].split(':')
                   temptime[0] = parseInt(temptime[0])+12
                   temptime = temptime.join(':')
                   return templist[0]+' '+temptime                   
               }
            }

使用效果如下:

posted @ 2019-11-27 15:10  醉梦者  阅读(1054)  评论(0编辑  收藏  举报