Problem
I have a string that looks like this:
var testDate = "Fri Apr 12 2013 19:08:55 GMT-0500 (CDT)"
Moment is something I’d like to employ. For display, acquire it in the format mm/dd/yyyy: 04/12/2013.
I tried utilizing this strategy, but it didn’t work out.
moment(testDate,'mm/dd/yyyy');
Which throws an error and claims there is no such thing as a replace method? Is it possible that I’m approaching this incorrectly?
I should also clarify that I’m utilizing a Meteor.js pre-packaged version of 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)
Asked by Warz
Solution #1
Instead of a display format, the second argument to moment() is a parsing format.
You’ll need the.format() method for this:
moment(testDate).format('MM/DD/YYYY');
It’s also worth noting that the context matters. The format for Month, Day of Month, and Year should all be in uppercase.
Answered by Jonathan Lonowski
Solution #2
Include moment.js and format your date using the code below.
var formatDate= 1399919400000;
var responseDate = moment(formatDate).format('DD/MM/YYYY');
“13/05/2014” is my output.
Answered by Akalya
Solution #3
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"
Answered by Avinash Sharma
Solution #4
Moment is a fantastic time manipulation library, but it is considered a legacy project, and the team advises that you use alternative libraries instead.
date-fns is one of the most lightweight libraries available; it’s modular, so you can pick and choose the functions you require, reducing the size of your bundle (issue & statement).
import { format } from 'date-fns' // 21K (gzipped: 5.8K)
import moment from 'moment' // 292.3K (gzipped: 71.6K)
With date-fns, you can format a date as follows:
// moment.js
moment().format('MM/DD/YYYY');
// => "12/18/2020"
// date-fns
import { format } from 'date-fns'
format(new Date(), 'MM/dd/yyyy');
// => "12/18/2020"
More on the cheat sheet, which includes a list of functions that can be used to replace the moment. js: You-Dont-Need-Momentjs
Answered by t_dom93
Solution #5
Use format to calculate the output date. The second moment parameter is for parsing; nonetheless, omitting it will result in a deprecation notice for testDate.
If you want to avoid this warning, supply a parsing format.
Answered by Kamil Kiełczewski
Post is based on https://stackoverflow.com/questions/15993913/format-date-with-moment-js