How to Install & Use Moment.js in Vue.js
In this guide, I’m going to show how to setup and use moment.js in Vue.js. Let’s get started:
Table of Contents
Installation
Run this command to install moment.js:
npm install moment --save # npm
yarn add moment # yarn
Examples
Now let’s see some examples.
Get current datetime:
<template>
<div id="app">
<p>{{currentDateTime()}}</p>
</div>
</template>
<script>
import moment from 'moment'
export default {
methods: {
currentDateTime() {
return moment().format('MMMM Do YYYY, h:mm:ss a')
}
}
};
</script>
Have a look at some usage of current datetime:
// an instance of momentjs
var current = moment()
// unix timestamp:
var current = moment().unix()
// as ISO
var current = moment().toISOString()
Check the date is today or not:
<template>
<div id="app">
<p>{{isToday("09-06-2020")}}</p>
</div>
</template>
<script>
import moment from 'moment'
export default {
methods: {
isToday(date) {
return moment(date).isSame(moment().clone().startOf('day'), 'd')
}
}
};
</script>
Change Language:
<template>
<div id="app">
<p>{{BDTime()}}</p>
</div>
</template>
<script>
import moment from 'moment'
export default {
methods: {
BDTime() {
moment.locale("bn") // bengali
return moment().format('LTS')
}
}
};
</script>
To get more examples, please check moment.js’s official website.
That’s it. Thanks for reading. ?Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)