This tutorial will show you how to use MomentJS in Laravel using examples. How to install Moment in Laravel Vue is explained step-by-step. We will assist you by providing a Laravel installation example. You may find a straightforward example of using MomentJS in Laravel in this article.
In this example, I'll walk you through installing Moment in Laravel Mix step-by-step. I'll give you two illustrations of how to install a moment in Laravel. Laravel Mix will be used in one example, which will use the npm command, and CDN Js in the other.
Moment is simple to utilize in versions of Laravel 6, Laravel 7, Laravel 8, and Laravel 9. So let's look at the technique below step by step.
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 that we have a new Laravel application, we must install npm. Let's simply execute the following command. this command will create "mode_modules" folder in your root directory and store all npm module there.
npm install
Following that, we must install the Font Awesome library using the npm command below. Let's execute the command below:
npm install moment
Use in our app.js file after a successful install. Let's import then as follows:
resources/js/app.js
require('./bootstrap'); var moment = require('moment'); console.log(moment().format());
We can now execute the npm dev command by executing the following command:
npm run dev
Here, we will use the produced app.css file as seen below in our blade file:
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? - CodeSolutionStuff.com</h1> </body> </html>
You can now launch your application and check the home page. You'll get the following output:
2020-06-10T19:58:17+05:30
Install Using CDNJS
Here, we'll utilize a cdn js file to add moment js, so take a look at the file code below:
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? - CodeSolutionStuff.com</h1>
<script type="text/javascript">
var cTime = moment().format();
console.log(cTime);
</script>
</body>
</html>