10

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
  • 115
  • 1
  • 1
  • 9

4 Answers4

18

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

Ziv Ben-Or
  • 1,149
  • 6
  • 15
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,741
  • 1
  • 16
  • 27
0
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

Rxhack com
  • 136
  • 1
  • 4