简体   繁体   English

Javascript格式日期/日期(-62135571600000)/

[英]Javascript format date /Date(-62135571600000)/

如何在JavaScript中将以下内容格式化为“ mm / dd / yyyy h:mm:ss”日期?

/Date(-62135571600000)/

In my current time zone: 在我当前的时区:

new Date(-62135571600000); //=> Mon Jan 01 1 02:00:00 GMT-0500 (Eastern Standard Time)

Is that what you're looking for? 那是您要找的东西吗? So you can easily pull out the date properties you want from that object to format it as you like... 因此,您可以轻松地从该对象中提取所需的日期属性,以根据需要设置其格式...

// create a new date object from the timestamp...
var p = (new Date(-62135571600000)).toISOString().split(/\D+/)
// format the date
var formatted = [p[1],p[2],p[0]].join("/")+" "+[p[3],p[4],p[5]].join(":")
// check it...
alert(formatted)

(new Date(-62135571600000)) returns a date object, which when output as a string looks like... Mon Jan 01 1 07:00:00 GMT+0000 (GMT) . (new Date(-62135571600000))返回一个日期对象,该日期对象以字符串形式输出时类似于... Mon Jan 01 1 07:00:00 GMT+0000 (GMT) Internally, javascript understands it as a date. 在内部,javascript将其理解为日期。 Next, we convert it .toISOString() , so the format looks more like... 0001-01-01T07:00:00.000Z - which is ISO standard date format. 接下来,我们将其转换为.toISOString() ,因此格式看起来更像是... 0001-01-01T07:00:00.000Z这是ISO标准日期格式。 Next, we split it into an array by any non-digit characters using regex ( .split(/\\D+/) ), which gives us something like... ["0001", "01", "01", "07", "00", "00", "000", ""] . 接下来,我们使用正则表达式( .split(/\\D+/) )将其拆分为任意非数字字符的数组,这给我们提供了类似... ["0001", "01", "01", "07", "00", "00", "000", ""] Finally, we assign that to a variable... var p = ... . 最后,我们将其分配给变量... var p = ...

Now we have the date parts in the p array, we can assemble them as we wish. 现在我们在p数组中有日期部分,我们可以根据需要组装它们。 Firstly, joining the parts 1, 2 and 0 (0 is year, 1 is month, 2 is day) with slashes ( [p[1],p[2],p[0]].join("/") ) giving 0001-01-01 . 首先,用斜杠( [p[1],p[2],p[0]].join("/") )连接部分1、2和0(0是年,1是月,2是日) [p[1],p[2],p[0]].join("/")0001-01-01 Next we add a space ...+" "+... and join the times together... [p[3],p[4],p[5]].join(":") . 接下来,我们添加一个空格...+" "+...并将时间加在一起... [p[3],p[4],p[5]].join(":") Assign the result to a variable... var formatted = ... and we are good to go! 将结果分配给变量... var formatted = ... ,我们一切顺利!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM