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

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().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"

其他回答

var moment = require('moment');

let yourdate = '2021-01-02T07:57:45.121Z'; // for example

moment(yourdate).format('MM/DD/YYYY');

// output : 01-02-2021


moment(yourdate).format('DD-MMM-YYYY');

// output : 01-Jan-2021
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和使用下面的代码,你可以格式化你的日期

var formatDate= 1399919400000;

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

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

你可以将“L”传递给format方法,它处理国际化…

moment.locale('en-US');
moment().format("L");
> "06/23/2021"

moment.locale('fr');
moment().format("L");
> "23/06/2021"

其他长日期格式(fr locale):

LT : 'HH:mm',
LTS : 'HH:mm:ss',
L : 'DD/MM/YYYY',
LL : 'D MMMM YYYY',
LLL : 'D MMMM YYYY HH:mm',
LLLL : 'dddd D MMMM YYYY HH:mm'

文档:https://momentjs.com/docs/#/displaying/format/(见“本地化格式”)

用于从输出日期使用格式。第二个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 " > < /脚本>