How to Use Moment JS in Laravel?

July 9, 2020 | Category : Laravel

Now, let's see post of use moment js in laravel. This post will give you simple example of install moment.js laravel. We will use use moment in laravel blade. let’s discuss about laravel mix moment js example. Let's get started with how to 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 and laravel 7 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? - HDTuto.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? - HDTuto.com</h1>

<script type="text/javascript">

var cTime = moment().format();

console.log(cTime);

</script>

</body>

</html>

I hope it can help you...