Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

javascript - 格式化日期与moment.js(format date with moment.js)

I have a string in this format:(我有一个这种格式的字符串:)

var testDate = "Fri Apr 12 2013 19:08:55 GMT-0500 (CDT)" I would like to using moment.js get it in this format mm/dd/yyyy : 04/12/2013 for display.(我想使用moment.js以这种格式获取它mm/dd/yyyy : 04/12/2013进行显示。) I tried to do it using this method,(我尝试使用这种方法,) moment(testDate,'mm/dd/yyyy'); Which errors and says there is no such method called replace ?(哪个错误并且说there is no such method called replace ?) am i approaching this in the wrong way?(我是以错误的方式接近这个吗?) Edit:(编辑:) I should also mention that i am using a pre packaged version of moment.js, packaged for meteor.js(我还要提一下,我正在使用为meteor.js打包的预打包版本的moment.js) Object [object Date] has no method 'replace' : The Exact error from the console Stack Trace:(堆栈跟踪:) 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)   ask by Warz translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The 2nd argument to moment() is a parsing format rather than an display format.(moment()的第二个参数是解析格式而不是显示格式。)

For that, you want the .format() method :(为此,您需要.format()方法 :) moment(testDate).format('MM/DD/YYYY'); Also note that case does matter.(还要注意案例确实很重要。) For Month, Day of Month, and Year, the format should be uppercase.(对于Month,Day和Year,格式应为大写。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...