javascript 使用 moment.js 格式化日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15993913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
format date with moment.js
提问by Warz
I have a string in this format:
我有一个这种格式的字符串:
var testDate = "Fri Apr 12 2013 19:08:55 GMT-0500 (CDT)"
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
? am i approaching this in the wrong way?
哪个错误并说there is no such method called replace
?我是否以错误的方式接近这个?
Edit:
编辑:
I should also mention that i am using a pre packaged version of moment.js, packaged for meteor.js
我还应该提到我使用的是 moment.js 的预打包版本,为meteor.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)
回答by Jonathan Lonowski
The 2nd argument to moment()
is a parsingformatrather than an displayformat.
的第二个参数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 of Month 和 Year,格式应为大写。
回答by Akalya
Include moment.js and using the below code you can format your date
包括 moment.js 并使用以下代码可以格式化您的日期
var formatDate= 1399919400000;
var responseDate = moment(formatDate).format('DD/MM/YYYY');
My output is "13/05/2014"
我的输出是“13/05/2014”
回答by Avinash Sharma
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"
回答by Kamil Kie?czewski
For fromating output date use format
. Second moment argument is for parsing - however if you omit it then you testDate
will cause deprecation warning
对于从输出日期使用format
. 第二时刻参数用于解析 - 但是,如果省略它,testDate
则会导致弃用警告
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format...
弃用警告:提供的值不是公认的 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>
to omit this warning you should provide parsing format
要忽略此警告,您应该提供解析格式
var testDate= "Fri Apr 12 2013 19:08:55 GMT-0500 (CDT)"
let s= moment(testDate, 'ddd MMM D YYYY HH:mm:ss ZZ').format('MM/DD/YYYY');
console.log(s);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>