js中怎么吧long型的日期转换成String类型的

JavaScript011

js中怎么吧long型的日期转换成String类型的,第1张

js中把long型的日期转换成String类型的代码如下

Date.prototype.format = function(f){

    var o ={

        "M+" : this.getMonth()+1, //month

        "d+" : this.getDate(),    //day

        "h+" : this.getHours(),   //hour

        "m+" : this.getMinutes(), //minute

        "s+" : this.getSeconds(), //second

        "q+" : Math.floor((this.getMonth()+3)/3),  //quarter

        "S" : this.getMilliseconds() //millisecond

    }

    if(/(y+)/.test(f))f=f.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length))

    for(var k in o)

        if(new RegExp("("+ k +")").test(f))f = f.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length))return f

}

以上代码用到正则表达式进行解决

正则表达式,又称规则表达式。计算机科学的一个概念。正则表通常被用来检索、替换那些符合某个模式(规则)的文本。

javascript 时间戳自定义格式转换,支持年月日时分秒等多种形式组合的日期和时间。

示例

年、月、日、时、分、秒

var date = jutils.formatDate(new Date(1533686888*1000),"YYYY-MM-DD HH:ii:ss")

console.log(date)

// 2019-07-09 19:44:01

更多自定义参数和用法可以参照:

javascript格式化日期时间

下面是源码的截图:

js时间戳转为日期格式

因为js是若数据类型的,你的这串数字152xxxx是字符串类型的,传到Date的构造方法里就变成了NaN了你可以先parseInt下把字符串转成数字就可以了

var string_timestamp = '1524800475367'// String时间戳

var nan = new Date(string_timestamp)

console.log(nan)

var time = new Date(parseInt(string_timestamp))

console.log(time)