How to Install and Use Moment JS in Laravel?

By Hardik Savani April 16, 2024 Category : Laravel

This tutorial will provide example of how to use moment js in laravel. step by step explain how to install moment in laravel vue. we will help you to give example of install moment in laravel. This article will give you simple example of use moment js in laravel.

In this example, i will show you step by step how to install moment in laravel mix. i will give you two example of installing moment in laravel. one will be using npm command using laravel mix and another example will using cdn js.

You can easily use moment in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version. so let's see bellow step be step process.

1) Install Using Npm

first, we will install laravel fresh version. so let's run bellow command:

composer create-project --prefer-dist laravel/laravel blog

Now, we need to install npm in our new laravel application. so let's simply run bellow command. this command will create "mode_modules" folder in your root directory and store all npm module there.

npm install

After that we need to install font awesome library using bellow npm command. so let's run bellow command:

npm install moment

After install successfully, we can use in our app.js file. so let's import as bellow:

resources/js/app.js

require('./bootstrap');

var moment = require('moment');

console.log(moment().format());

Now we are ready to run npm dev command, so let's run bellow command:

npm run dev

Here, we will use generated app.css file in our blade file as bellow:

resources/views/welcome.blade.php

<!DOCTYPE html>

<html>

<head>

<title></title>

<link type="text/css" rel="stylesheet" href="{{ mix('css/app.css') }}">

<script type="text/javascript" src="{{ mix('js/app.js') }}"></script>

</head>

<body>

<h1>How to use moment js in Laravel? - ItSolutionStuff.com</h1>

</body>

</html>

Now you can run your application and see on home page. You will get output as bellow:

2020-06-10T19:58:17+05:30

2) Install Using CDNJS

here, we will use cdn js file for adding moment js, so let see bellow file code:

resources/views/welcome.blade.php

<!DOCTYPE html>

<html>

<head>

<title></title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.26.0/moment.min.js"></script>

</head>

<body>

<h1>How to use moment js in Laravel? - ItSolutionStuff.com</h1>

<script type="text/javascript">

var cTime = moment().format();

console.log(cTime);

</script>

</body>

</html>

I hope it can help you...

Tags :
Shares