Questions tagged [momentjs]

A JavaScript date library for parsing, manipulating, and formatting dates. Use this tag for issues with moment, moment-timezone, and any moment plugins. NOTE: As of September 2020, the Moment team recommends you chose a different library for new projects. See the Project Status section of the Moment docs. On StackOverflow, please refrain from recommending Moment as a solution unless Moment is specifically asked for in the question.

Moment.js is a JavaScript date library for parsing, manipulating, and formatting dates. It is design to work in both the browser and in NodeJS. Moment supports dates in all standard formats, locales, relative time, and time zones.


Important:

As of September 2020, the Moment team recommends you choose a different library than Moment for new projects. Please read https://momentjs.com/docs/#/-project-status/

On StackOverflow, please refrain from recommending Moment as a solution unless Moment is specifically asked for in the question.


Resources

Basic examples

Parsing in a specific format

moment("04/07/2013","MM/DD/YYYY")

Current date in default format

moment().format()

Current date in a custom format

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

Manipulation, calendar time

moment().subtract('days', 3).calendar()

Internationalization

moment.locale('fr');
moment().format('LLLL')

Common Issues

  • Do not attempt to use the internal fields, which are prefixed with underscores, such as _i or _d. They must be interpreted in a specific way to be useful. Instead, use the public API functions, such as .format() and others.

  • Don't forget that moment objects are mutable. For example:

     var a = moment('2015-01-01');
     var b = moment.year(2000);
    

    Now, both b and a are set to the same moment object, having year 2000. For these to be separate objects, you must first clone the moment.

     var a = moment('2015-01-01');
     var b = moment.clone().year(2000);
    

    You can also clone a moment by passing a moment into the moment constructor.

     var a = moment('2015-01-01');
     var b = moment(a).year(2000);
    
  • Remember that like the Date object, months are 0-11 when passed numerically, such as with the month function, or when passing an array to the moment constructor.

Related Tags

8106 questions
903
votes
11 answers

Moment.js transform to date object

Using Moment.js I can't transform a correct moment object to a date object with timezones. I can't get the correct date. Example: var oldDate = new Date(), momentObj = moment(oldDate).tz("MST7MDT"), newDate =…
vadim.zhiltsov
  • 9,294
  • 2
  • 15
  • 16
544
votes
17 answers

Get hours difference between two dates in Moment Js

I'm able to get the difference between two dates using MomentJs as follows: moment(end.diff(startTime)).format("m[m] s[s]") However, I also want to display the hour when applicable (only when >= 60 minutes have passed). However, when I try to…
Dani
  • 5,828
  • 2
  • 17
  • 21
531
votes
9 answers

Moment js date time comparison

I'm using moment.js to format my date time, here I have two date values, and I want to achieve a particular function when one date is greater than the other. I read most of their docs, but didn't find the function to achieve this. I know it will be…
Dibish
  • 9,133
  • 22
  • 64
  • 106
367
votes
9 answers

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 use Moment.js get it in this format mm/dd/yyyy : 04/12/2013 for display. I tried to do it using this…
Warz
  • 7,386
  • 14
  • 68
  • 120
362
votes
18 answers

How can I remove time from date with Moment.js?

formatCalendarDate = function (dateTime) { return moment.utc(dateTime).format('LLL'); }; It displays: "28 februari 2013 09:24" But I would like to remove the time at the end. How can I do that? I'm using Moment.js.
Obsivus
  • 8,231
  • 13
  • 52
  • 97
331
votes
18 answers

Moment JS - check if a date is today or in the future

I am trying to use momentjs to check if a given date is today or in the future. This is what I have so far: