hulkenbergg
hulkenbergg

Reputation: 115

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>

Upvotes: 10

Views: 43939

Answers (4)

Rxhack com
Rxhack com

Reputation: 156

showDate()
{
   const dateObj = new Date();
   const currentDate = dateObj.getDate()+"/"+dateObj.getMonth()+"/"+dateObj.getFullYear();
   console.log(currentDate);
},

use this process to get current date in any required format

Upvotes: 0

Faridul Khan
Faridul Khan

Reputation: 2007

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

Upvotes: 1

Ziv Ben-Or
Ziv Ben-Or

Reputation: 1194

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

Upvotes: 18

Hamouda Arfaoui
Hamouda Arfaoui

Reputation: 70

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>

Upvotes: 1

Related Questions