Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get full current date and time in Vue.js without writing massive JavaScript lines

I want to print "Last updated: 6/12/2019 1:30 PM" in Vue.js. I have the current year but I want the full date and time. Here's what I have so far

<div id="app">
Last updated: {{ new Date().getFullYear() }}
</div>
like image 482
hulkenbergg Avatar asked Jun 12 '19 18:06

hulkenbergg


3 Answers

You can use: new Date().toLocaleString(), it gives you "6/13/2019, 9:40:40 AM"

like image 84
Ziv Ben-Or Avatar answered Nov 20 '22 05:11

Ziv Ben-Or


It is a very simple getting current date time in vue.js.

You can use: {{ new Date() }}, it gives you Sat Dec 05 2020 12:51:25

Date() will provide us full date and time with timezone.

But you want to make better format like 'dd-mm-yyyy' in vue.js, then you use moment

import moment from 'moment'

Vue.prototype.moment = moment

Then in your template, simply use

{{ moment(new Date()).format('DD-MM-YYYY') }}

Output:

05-12-2020
like image 36
Faridul Khan Avatar answered Nov 20 '22 06:11

Faridul Khan


you can either use JavaScript new Date() methods check MDN , or you can install vue-moment via npm .

Installation :

npm install moment

Require the plugin :

var moment = require('moment');

Display Date :

<span>Last updated: {{ moment("D/M/YYYY h:mm A") }}</span>
like image 40
Hamouda Arfaoui Avatar answered Nov 20 '22 06:11

Hamouda Arfaoui