Integrating the MomentJs library into Lightning Components

MomentJs is an open source library that provides utility functions to handle date and time. The library works well with Salesforce Locker Service.

Here is an example component showing how to integrate MomentJs into Lightning Components. The steps to integrate the library are as follows: 

  1. Create a static resource hosting MomentJs: The source code for the library can be obtained from https://MomentJs.com/downloads/Moment.Js. Download and create a static resource file named MomentJs. 
  1. Use the ltng:require tag and leverage the afterScriptsLoaded event to load the JavaScript files and styles. The following is simple component code that demonstrates the use of the ltng:require tag:

Get Learning Salesforce Lightning Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.