mos*_*toh -2 javascript jquery
我的代码是这样的:
var createdDate = '2013-01-21 01:23:44';
createdDate = new Date(createdDate);
date = createdDate.toLocaleDateString();
time = createdDate.toLocaleTimeString().replace(/(.*)\D\d+/, '$1');
console.log(date+' '+time);
Run Code Online (Sandbox Code Playgroud)
结果:1/21/2013 1:23 AM
我要结果:2013-01-21 1:23 AM
有什么解决方案可以解决我的问题?
当然,有一些简单的解决方案,但在 JS 中处理日期/时间通常很痛苦。如果您打算以任何严肃的身份处理日期/时间,我强烈建议您使用Moment.js的格式方法,因为它的健壮性和灵活性,它应该能够做您想做的事。
文档中的示例:
moment().format(); // "2014-09-08T08:02:17-05:00" (ISO 8601)
moment().format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, February 14th 2010, 3:25:50 pm"
moment().format("ddd, hA"); // "Sun, 3PM"
moment('gibberish').format('YYYY MM DD'); // "Invalid date"
Run Code Online (Sandbox Code Playgroud)