moment.js

Why Moment.js is best compared to JavaScript Date Object?

Why Moment.js is easy to use in comparison to using Javascript Date Object?

Let’s read ahead…

What is Moment.js?

Moment.js is an open-source JavaScript library that eliminates the need to utilize the local JavaScript Date object directly. The library is built with a Javascript Date object (in the same way that jQuery is a library built with JavaScript) making the object a whole lot easier to work with.

The issue with the JavaScript Date object isn’t the usefulness it brings, it’s simply very awkward to use as a developer. To do complex parsing, validation, and displaying of dates, you’ll end up writing a great deal of code. Additionally, Moment.js extends native JavaScript Date capabilities with several features, such as relative time, calendar time, durations, and multi-language support.

It has an endless list of plugins that allow for some features like time-zone support, recurrence, and Twitter integration.

Why Moment.js?

It works for the client-side as well as for the back-end side. I truly believe that it’s the best JavaScript library to use rather than the Date object.

It upholds internationalization and localization which is especially significant for frontend developers. This means that they don’t need to provide support (like creating interpretation files) for extra interpretations for languages that are as of now executed in our applications.

The bundle of available options is stunning: Most probably, it provides everything that a frontend developer might use in an application. Truly, I can’t sort out a situation in which I would need to override or expand its functionalities.

Hire NodeJs Developer

Installation or Setup Moment.js

Browser

You can either download the JS file from the official website or use cdnjs.com

With local JS file

<script src="moment.js"></script>
<script>
     moment().format();
</script>

With CDN

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js">
</script>
<script>
     moment().format();
</script>

NodeJs

npm install moment

A moment should be included inside the node_modules directory, Then add it to your script.

var moment = require('moment');
moment();

javascript

Examples of Moment.js

  • To get the current date and time, just call moment() with no parameters
    • var now = moment();
  • String
    • var day = moment(“2022-02-14”);
  • String + Format
    • moment(“02-08-2022”, “MM-DD-YYYY”);
  • String + Formats
    • moment(“08-02-2022”, [“MM-DD-YYYY”, “DD-MM”, “DD-MM-YYYY”])
      Special Formats
    • moment(“2022-01-01T05:06:07”, moment.ISO_8601);
    • moment(“2022-01-01T05:06:07”, [“YYYY”, moment.ISO_8601]);
  • Object
moment({ hour:15, minute:10 });
moment({ y: 2022, M: 2, d: 8, h:15, m: 10, s: 3, ms: 123 });
moment({ year: 2022, month: 2, day: 8, hour: 15, minute: 10, second: 3, millisecond: 123 });
moment({ years: 2022, months: 2, days: 8, hours: 15, minutes: 10, seconds: 3, milliseconds: 123 });
moment({ years: 2022, months: 2, date: 8, hours: 15, minutes: 10, seconds: 3, milliseconds: 123 });
moment({ years: 2022, months: '2', date: '8', hours: '15', minutes: '10', seconds: '3', milliseconds: '123'});
  • Unix Timestamp
    • var day = moment.unix(1644818693);
  • Date
    • var day = new Date(2022, 02, 08); var dayWrapper = moment(day);
  • Array
    • moment([2022, 1, 14, 15, 25, 50, 125]); // February 14th, 3:25:50.125 PM
  • UTC
    • moment().format(); // 2022-02-08T10:35:24-08:00
    • moment.utc().format(); // 2022-02-08T18:35:24+00:00
  • parseZone
    • moment.parseZone(“2022-02-08T00:00:00-13:00”).utcOffset(); // -780 (“-13:00” in total minutes)
    • moment.parseZone(‘2022 02 08 05 -13:00’, ‘YYYY MM DD HH ZZ’).utcOffset(); // -780 (“-13:00” in total minutes)
    • moment.parseZone(‘2022-02-08-13:00’, [‘DD MM YYYY ZZ’, ‘YYYY MM DD ZZ’]).utcOffset(); // -780 (“-13:00” in total minutes);
  • Validations
    • new Date(2022, 25, 14).toString(); // “Sat Feb 14 2022 00:00:00 GMT-0500 (EST)”
    • moment([2022, 25, 35]).format(); // ‘Invalid date’

Invalid Moments.js

  • invalid.add(unit, value)
  • another.add(invalid)
  • invalid.clone()
  • invalid.diff(another)
  • invalid.endOf(unit)
  • invalid.max(another)
  • another.max(invalid)
  • invalid.min(another)
  • another.min(invalid)
  • invalid.set(unit, value)
  • invalid.startOf(unit)
  • invalid.subtract(unit, value)
  • invalid.format(anyFmt)
  • invalid.from(another)
  • another.from(invalid)
  • invalid.fromNow(suffix)
  • invalid.to(another)
  • another.to(invalid)
  • invalid.toNow(suffix)
  • invalid.toISOString()
  • invalid.toString()
  • invalid.isAfter(another)
  • invalid.isAfter(invalid)
  • another.isAfter(invalid)
  • invalid.isBefore(another)
  • invalid.isBefore(invalid)
  • another.isBefore(invalid)
  • invalid.isBetween(another, another)
  • invalid.isBetween(invalid, invalid)
  • invalid.isSame(another)
  • invalid.isSame(invalid)
  • another.isSame(invalid)
  • invalid.isSameOrAfter(another)
  • invalid.isSameOrAfter(invalid)
  • another.isSameOrAfter(invalid)
  • invalid.isSameOrBefore(another)
  • invalid.isSameOrBefore(invalid)
  • another.isSameOrBefore(invalid)
  • invalid.get(unit) returns null
  • invalid.toArray() === [NaN, NaN, NaN, NaN, NaN, NaN]
  • invalid.toObject() has all values set to NaN
  • invalid.toDate() returns an invalid Date
  • invalid.toJSON() returns null
  • invalid.unix() returns null
  • invalid.valueOf() returns null
  • invalid.toISOString() returns null

 

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply