-2

Get the Current Timestamp using js and try to append the current date, time, seconds, milliseconds in a single string

For Example: 2912019121216174

29-1-2019 -This Represents the current date. 12:12:16:174 - this Represents the current time,seconds and its milliseconds.

Sivakumar
  • 108
  • 11

1 Answers1

2

Hurrah!! Within few minutes i got the excepted result what i have except.

HTML

<p id="demo"></p>

JavaScript

var currentDate = new Date();

var date = currentDate.getDate();
var month = currentDate.getMonth(); //Be careful! January is 0 not 1
var months = (month+1);
var year = currentDate.getFullYear();
var hours = currentDate.getHours();
var minutes = currentDate.getMinutes();
var seconds = currentDate.getSeconds();
var milliseconds = currentDate.getMilliseconds();

var dmy = (""+date+months+year+hours+minutes+seconds+milliseconds);

  document.getElementById("demo").innerHTML = dmy;
Sivakumar
  • 108
  • 11
  • What are you using this for? Date returns a `ticks` or milliseconds since a set date. If you are after a unique ID then this would be better. – Bibberty Jan 29 '19 at 07:02
  • Hi Bibberty, I am using this for Unique ID. – Sivakumar Jan 29 '19 at 07:05
  • Hello @Sivakumar, you can just use UUIDv4 for unique id purposes. Check this answers: https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript https://stackoverflow.com/questions/1155008/how-unique-is-uuid – alpay Jan 29 '19 at 07:16
  • And is you really need Date.now() which returns milliseconds. – Bibberty Jan 29 '19 at 07:20
  • Yes Bibberty, Date.now() gives the unique value but i cant able get the exact what i need so i used above method to get the unique id. – Sivakumar Jan 29 '19 at 07:29