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

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()方法:

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

还要注意,大小写确实很重要。对于Month、Day of Month和Year,格式应为大写。

其他回答

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

你可以将“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/(见“本地化格式”)

获取当前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

moment()的第二个参数是一种解析格式,而不是显示格式。

为此,你需要.format()方法:

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

还要注意,大小写确实很重要。对于Month、Day of Month和Year,格式应为大写。

这可能有助于一些人谁正在寻找多种日期格式一个接一个地自愿或意外。 请查看代码: 我在当前日期上使用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