- MomentJS - Examples
- MomentJS - Plugins
- MomentJS - Utilities
- MomentJS - Durations
- MomentJS - Customization
- MomentJS - Internationalization
- MomentJS - Date Queries
- Formatting Date And Time
- Manipulate Date And Time
- MomentJS - Getter/Setter
- MomentJS - Date Validation
- MomentJS - Parsing Date And Time
- MomentJS - Introduction
- MomentJS - Environment Setup
- MomentJS - Overview
- MomentJS - Home
MomentJS Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
MomentJS - Environment Setup
In this chapter, you will learn in detail about setting up the working environment of MomentJS on your local computer. Before you begin with working on MomentJS, you need to have the access to the pbrary. You can access its files in any of the following methods −
Method 1: Using MomentJS File in Browser
In this method, we are going to need MomentJS file from its official website and will use it directly in the browser.
Step 1
As a first step, go to the official website of MomentJS
You will find the home page as shown here −Observe that there is a download option available which gives you the latest MomentJS file available. Note that the file is available with and without minification.
Step 2
Now, include moment.js inside the script tag and start working with MomentJS. For this, you can use the code given below −
<script type = "text/JavaScript" src = " https://MomentJS.com/downloads/moment.js"></script>
Given here is a working example and its output for a better understanding −
Example
<html> <head> <title>MomentJS - Working Example</title> <script type = "text/JavaScript" src = "https://MomentJS.com/downloads/moment.js"></script> <style> span { border: sopd 1px #ccc; padding:10px; font-family: "Segoe UI",Arial,sans-serif; width: 50%; } </style> </head> <body> <span style = "font-size:25px" id = "todaysdate"></span> <script type = "text/JavaScript"> var a = moment().toString(); document.getElementById("todaysdate").innerHTML = a; </script> </body> </html>
Output
The moment-locale file to work with different locales is also available as shown in the screenshot above. Now, add the file to the script tag as shown below and work with different locales of your choice. For this, you can use the code given below −
<script type="text/JavaScript" src="https://MomentJS.com/downloads/moment-with-locales.js"></script>
Given here is a working example for moment-locale and its output for a better understanding −
<html> <head> <script type = "text/JavaScript" src ="https://MomentJS.com/downloads/moment-with-locales.js"></script> </head> <body> <h1>Moment Locale</h1> <span id = "datedisplay" style = "font-size:30px;"></span> <script type = "text/JavaScript"> var a = moment.locale("fr"); var c = moment().format("LLLL"); document.getElementById("datedisplay").innerHTML = c; </script> </body> </html>
Output
Method 2: Using Node.js
If you are opting for this method, make sure you have Node.js and npm installed on your system. You can use the following command to install MomentJS −
npm install moment
You can observe the following output once MomentJS is successfully installed −
Now, to test if MomentJS works fine with Node.js, create the file test.js and add the following code to it −
var moment = require( moment ); var a = moment().toString(); console.log(a);
Now, in the command prompt, run the command node test.js as shown in the screenshot given below −
Note that this command displays the output for moment().toString().
Method 3: Using Bower
Bower is another method to get the required files for MomentJS. You can use the following command to install MomentJS using Bower −
bower install --save moment
The screenshot given below shows the installation of MomentJS using Bower −
These are the files loaded from Bower for MomentJS to install. The installed moment and locale files are shown in the image given below −
Advertisements