0

I am trying to get a string with date/time on this format: "YYYY-MM-DD-HH-MM" I cannot find a proper way to do it?

This is what I have at the moment:

var x = new Date();
var TimeStamp = x.toString();

Outputs:

Tue Oct 30 2018 14:33:03 GMT+0000 (UTC)

Martik
  • 405
  • 4
  • 16

1 Answers1

2

Use Date methods and format it.

var date = new Date();
var output = date.getFullYear()+'-'+("0" + (date.getMonth() + 1)).slice(-2)+'-'+("0" + date.getDate()).slice(-2)+'-'+date.getHours()+'-'+date.getMinutes()
console.log(output)

Or you can use momentjs format.

console.log(moment().format('YYYY-MM-DD-HH-mm'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>
Durga
  • 13,489
  • 2
  • 19
  • 40