5

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>
hulkenbergg
  • 65
  • 1
  • 1
  • 8

3 Answers3

11

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

Ziv Ben-Or
  • 1,009
  • 6
  • 14
1

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>
1

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
Faridul Khan
  • 1,069
  • 1
  • 12
  • 23