Coder Perfect

With Moment.js, you can format dates in a variety of ways.


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.


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 (
    at moment (
    at populateProfileForEdit (
    at Object.Template.profile_personal.rendered (
    at Spark.createLandmark.rendered (
    at Array.forEach (native)
    at Function._.each._.forEach (
    at _.extend.flush ( 

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:


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
// => "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