我有一个这样格式的字符串:

var testDate = "Fri Apr 12 2013 19:08:55 GMT-0500 (CDT)"

我想使用Moment.js以mm/dd/yyyy: 04/12/2013的格式获取它来显示。

我试着用这个方法,

moment(testDate,'mm/dd/yyyy');

哪个错误并说没有这样的方法称为替换?我用错方法了吗?


Edit

我还应该提到,我使用的是一个预先打包的Moment.js版本,它是为Meteor.js打包的

Object [object Date] has no method 'replace' : The Exact error from the console

堆栈跟踪:

 at makeDateFromStringAndFormat (http://127.0.0.1:3000/packages/moment/lib/moment/moment.js?b4e3ac4a3d0794023a4410e7941c3e179398b5b0:539:29)
    at moment (http://127.0.0.1:3000/packages/moment/lib/moment/moment.js?b4e3ac4a3d0794023a4410e7941c3e179398b5b0:652:24)
    at populateProfileForEdit (http://127.0.0.1:3000/client/views/home/administration/directory/profiles/profiles.js?acfff908a6a099f37312f62892a22b40f82e5e0f:147:25)
    at Object.Template.profile_personal.rendered (http://127.0.0.1:3000/client/views/home/administration/directory/profiles/profiles.js?acfff908a6a099f37312f62892a22b40f82e5e0f:130:13)
    at Spark.createLandmark.rendered (http://127.0.0.1:3000/packages/templating/deftemplate.js?b622653d121262e50a80be772bf5b1e55ab33881:126:42)
    at http://127.0.0.1:3000/packages/spark/spark.js?45c746f38023ceb80745f4b4280457e15f058bbc:384:32
    at Array.forEach (native)
    at Function._.each._.forEach (http://127.0.0.1:3000/packages/underscore/underscore.js?867d3653d53e9c7a171483edbcad9670e12288c7:79:11)
    at http://127.0.0.1:3000/packages/spark/spark.js?45c746f38023ceb80745f4b4280457e15f058bbc:382:7
    at _.extend.flush (http://127.0.0.1:3000/packages/deps/deps.js?9642a93ae1f8ffa8eb1c2475b198c764f183d693:231:11) 

当前回答

用于从输出日期使用格式。第二个moment参数是用于解析的——但是如果忽略了它,那么testDate将引起弃用警告

弃用警告:所提供的值不是公认的RFC2822或ISO格式…

var testDate= “Fri Apr 12 2013 19:08:55 GMT-0500 (CDT)” let s= moment(testDate).format('MM/DD/YYYY'); msg.innerText= s; <script src=“https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js”></script> <div id=“msg”></div>

要忽略此警告,您应该提供解析格式

var testDate= " 2013年4月12日星期五19:08:55 GMT-0500 (CDT)" let s= moment(testDate, 'ddd MMM D YYYY HH:mm:ss ZZ').format(' mm /DD/YYYY'); console.log(年代); < script src = " https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js " > < /脚本>

其他回答

包括moment.js和使用下面的代码,你可以格式化你的日期

var formatDate= 1399919400000;

var responseDate = moment(formatDate).format('DD/MM/YYYY');

我的输出是“13/05/2014”

moment().format();                                // "2019-08-12T17:52:17-05:00" (ISO 8601, no fractional seconds)
moment().format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Monday, August 12th 2019, 5:52:00 pm"
moment().format("ddd, hA");                       // "Mon, 5PM"

这可能有助于一些人谁正在寻找多种日期格式一个接一个地自愿或意外。 请查看代码: 我在当前日期上使用moment.js格式函数(今天是29-06-2020) var startDate = moment(new Date()).format('MM/DD/YY');结果:06/28/20

发生的事情是它只保留了年份部分:20作为“06/28/20”,如果我运行语句: 新日期(startDate可以) 结果是“周一6月28日格林威治时间1920 00:00:00 +0530(印度标准时间)”,

Then, when I use another format on "06/28/20": startDate = moment(startDate ).format('MM-DD-YYYY'); Result: 06-28-1920, in google chrome and firefox browsers it gives correct date on second attempt as: 06-28-2020. But in IE it is having issues, from this I understood we can apply one dateformat on the given date, If we want second date format, it should be apply on the fresh date not on the first date format result. And also observe that for first time applying 'MM-DD-YYYY' and next 'MM-DD-YY' is working in IE. For clear understanding please find my question in the link: Date went wrong when using Momentjs date format in IE 11

用于从输出日期使用格式。第二个moment参数是用于解析的——但是如果忽略了它,那么testDate将引起弃用警告

弃用警告:所提供的值不是公认的RFC2822或ISO格式…

var testDate= “Fri Apr 12 2013 19:08:55 GMT-0500 (CDT)” let s= moment(testDate).format('MM/DD/YYYY'); msg.innerText= s; <script src=“https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js”></script> <div id=“msg”></div>

要忽略此警告,您应该提供解析格式

var testDate= " 2013年4月12日星期五19:08:55 GMT-0500 (CDT)" let s= moment(testDate, 'ddd MMM D YYYY HH:mm:ss ZZ').format(' mm /DD/YYYY'); console.log(年代); < script src = " https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js " > < /脚本>

获取当前UTC时间,格式为YYYY-MM-DD HH:MM:ss。毫秒带时区,使用时刻格式,如下所示

moment().utc().format('Y-MM-DD HH:mm:ss.SSS Z').  

输出

2022-09-20 15:28:39.446 +0000