The Wayback Machine - https://web.archive.org/web/20240718061618/https://www.geeksforgeeks.org/moment-js-using-with-browser/
Open In App

Moment.js using with Browser

Last Updated : 06 Jul, 2022
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Moment.js is a date library for JavaScript that parses, validates, manipulates, and formats dates. In this article, we are going to see how we can use moment.js in the browser.

Approach: To be able to use moment.js in the browser you will need to include a script tag in the head tag to import the library. We can use any of the following CDNs for including Moment.js.

CDN Links:

You can visit https://cdnjs.com/libraries/moment.js and https://www.jsdelivr.com/package/npm/moment to get the CDN file link.

<script src=”https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment-with-locales.min.js”></script>

<script src=”https://cdn.jsdelivr.net/npm/moment@2.29.3/moment.min.js”></script>

The below examples will help to understand the approach for using Moment.js in the browser.

Example 1:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <button type="button" onclick="clickHandler()">
        Get Today!
    </button>
  
    <script>
        function clickHandler() {
            alert(moment()
                .format('dddd, Do MMM YYYY, h:mm:ss A'))
        }
    </script>
</body>
  
</html>


Output:

 

Example 2:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <button type="button" onclick="clickHandler()">
        Click Here!!!
    </button>
  
    <script>
        function clickHandler() {
            let day = "2010-02-28 13:40:55"
            let date = moment(day);
            alert(date.format("DD/MM/YYYY-H:mm:ss"));
        }
    </script>
</body>
  
</html>


Output:

 

Reference: https://momentjs.com/docs/#/use-it/browser/



Similar Reads

Moment.js moment().max(Moment|String|Number|Date|Array) Method
The moment().max() method limits the moment to a maximum of another moment value. We can also say that m1.max(m2) is the same as m1 = moment.min(m1, m2). This method is the counterpart of moment.min() method. Syntax: moment().max(Moment|String|Number|Date|Array); Parameters: This method can accept the following parameters as mentioned above and des
3 min read
Moment.js moment().min(Moment|String|Number|Date|Array) Method
The moment().min() method limits the moment to a minimum of another moment value. We can also say that m1.min(m2) is the same as m1 = moment.max(m1, m2). This method is the counterpart of moment.max() method. Syntax: moment().min(Moment|String|Number|Date|Array); Parameters: This method can accept the following parameters as mentioned above and des
3 min read
Moment.js moment().week() Method
The moment().week() method is used to get or set the week of the Moment object. It is a locale aware method and hence the week of number will differ based on the locale of the Moment. This varies as the first day of the week can be either a Sunday or Monday according to the Locale. Note: The day of the week would stay the same when this method is u
3 min read
Moment.js moment().toString() Method
The moment().toString() Method is used to return a human-readable string of the Moment object in English. It is similar to the native Date object's toString() method. The date return contains the Weekday, Month, Day, Year, Time and Timezone Offset. Syntax: moment().toString(); Parameters: This method does not accept any parameters: Return Value: Th
2 min read
Moment.js moment().weekYear() Method
The moment().weekYear() Method is used to get or set the current week-year of the Moment. This method is Locale-aware meaning that the first day of the week can be a Sunday or Monday depending on the Locale of the Moment. This may cause the week-year to differ as the first day of the first week may not fall on the first day of the year depending on
2 min read
Moment.js moment().local() Method
The moment().local() method is used to specify that the given Moment object's timezone would be displayed in the local timezone of the user. An optional parameter can be passed that preserves the current time value and only changes the timezone to the local one. Syntax: moment().local( Boolean ); Parameters: This method accepts a single parameter a
2 min read
Moment.js moment().from() Function
The moment().from() function is used to calculate the time from any other time in Node.js. It takes a date as a parameter and calculates the time from that date. Syntax: moment().from(Moment|String|Number|Date|Array, Boolean); Parameters: This function accepts two parameters, first is the date parameter which can be of Moment|String|Number|Date|Arr
2 min read
Moment.js moment().format() Function
The moment().format() function is used to format the date according to the user's need. The format can be provided in string form which is passed as a parameter to this function. Syntax: moment().format(String); Parameters: This function accepts a single parameter of string type, which defines the format. Return Value: This function returns the dat
2 min read
Moment.js moment().daysInMonth() Function
The moment().daysInMonth() function is used to get the number of days in the month of a particular month in Node.js. It returns an integer denoting the number of days. Syntax: moment().daysInMonth(); Parameters: This function has no parameters. Return Value: This function returns an integer. Installation of moment module: You can visit the link to
2 min read
Moment.js moment().valueOf() Function
The moment().valueOf() function is used to get the number of milliseconds since the Unix Epoch. Basically, Unix time is a system for describing a point in time. Syntax: moment().valueOf(); Parameters: This function has no parameters. Return Value: This function returns the Unix Timestamp in milliseconds. Installation of moment module: You can visit
2 min read
three90RightbarBannerImg