-1

I need to get the date time in years,month,day,hours,minute,seconds,milliseconds in this format

201802281007475001

right now i managed to return this:

2018418112252159

This is my code:

var date = new Date();
blockid = JSON.stringify(date.getFullYear()) + JSON.stringify(date.getMonth()+1) + JSON.stringify(date.getDate()) + JSON.stringify(date.getHours()) + JSON.stringify(date.getMinutes()) + JSON.stringify(date.getSeconds()) + JSON.stringify(date.getMilliseconds())

I also need to put a zero in front of the month and the day in case it is minor than 10.

Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
Gargantua
  • 420
  • 2
  • 5
  • 17

1 Answers1

3

One simple way to this is to call toISOString on the date object and replace the non-required characters with blank:

var date = new Date();
console.log(date.toISOString().replace(/\D/g, ''));
31piy
  • 23,323
  • 6
  • 47
  • 67