【JavaScript】时间转时间戳,时间戳转时间的常用方法

时间戳转时间
方法一:
使用:

timeto(152425542,"ymd-hms");

//获取时间戳转当前时间 年月日,时分秒 function timeto(date,type) { var seperator1 = "-"; var seperator2 = ":"; var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } if(type == "ym"){ // 转年月 var currentdate = date.getFullYear() + seperator1 + month; }else if(type == "ymd"){ // 转年月日 var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate; }else if(type == "ymd-hms"){ //转年月日 时分秒 var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds(); }else if(type == "hms"){ //转时分秒 var currentdate =date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds(); } return currentdate; }

方法二:

//时间戳转时间 yy-mm-ddgetTimes(time)//时间戳转时间 yy-mm-dd hh:mm:ssgetTime(time)

//时间戳转时间 function updatetime(a){ return a<10?"0"+a:a; } Date.prototype.toLocaleString = function() { return this.getFullYear() + "-" + updatetime((this.getMonth() + 1)) + "-" + updatetime(this.getDate()) + " " + updatetime(this.getHours()) + ":" + updatetime(this.getMinutes()) + ":" + updatetime(this.getSeconds()); }; Date.prototype.toLocaleStrings = function() { return this.getFullYear() + "-" + updatetime((this.getMonth() + 1)) + "-" + updatetime(this.getDate()); }; Date.prototype.toLocaleStringm = function() { return this.getFullYear() + "-" + updatetime((this.getMonth() + 1)); }; function getTime(time){ if(isUndefined(time)){ return ; }else{ var unixTimestamp = new Date( time ) ; commonTime = unixTimestamp.toLocaleString(); return commonTime; } }// function getTimes(time){ if(isUndefined(time)){ return ; }else{ var unixTimestamp = new Date( time ) ; commonTime = unixTimestamp.toLocaleStrings(); return commonTime; } } function getTimem(time){ if(isUndefined(time)){ return ; }else{ var unixTimestamp = new Date( time ) ; commonTime = unixTimestamp.toLocaleStringm(); return commonTime; } }

时间转时间戳
【【JavaScript】时间转时间戳,时间戳转时间的常用方法】方法一:
//转换毫秒 function getTimesByDate(datestr,type){ //type(0-yyyy-MM-dd,1-yyyy-MM-dd h:m:s); if(isUndefined(datestr)){return ""}; var dp="-"; var date_ar=datestr.split(dp); var y=Number(date_ar[0]); var m=Number(date_ar[1]); var d=Number(date_ar[2]); var theDate=new Date(); theDate.setFullYear(y,m-1,d); return theDate.getTime(); }

    推荐阅读