Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current date upto milliseconds using JavaScript?

Is there any method provided by JavaScript to get the current date down to milliseconds? I am using new Date() method but this returns the time till seconds as follows:

console.log(new Date())
LOGS - Thu Sep 07 2017 14:47:37 GMT+0530 (India Standard Time)

What I need is like: Thu Sep 07 2017 15:10:46:900100 GMT+0530 (India Standard Time)

However I need time up to milliseconds, that too upto 6 digits. Does JavaScript provide us any method to extract milliseconds? I know we can extract individual values like hours, minutes and milliseconds etc and append them again but that is not my requirement as I need the date in default JavaScript DATE format like mentioned above. So looking for any solutions related to that.

like image 264
Peter Avatar asked Oct 22 '25 15:10

Peter


2 Answers

var now = new Date();  //Fri Jul 24 2020 11:41:49 GMT+0530 (India Standard Time)
   

var nowIso = now.toISOString(); //"2020-07-24T06:11:49.911Z"
   

var nowInMilliseconds = Date.parse(nowIso); //1595571109911
like image 155
Adrika Gupta Avatar answered Oct 25 '25 04:10

Adrika Gupta


You can try the below code for your query :

    <!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>
<body>
<button onclick="myFunction()">click</button>
<p id="d"></p>
</body>
<script type="text/javascript">
    function myFunction() {
    var today = new Date();
    var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
    var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds() + ":" + today.getMilliseconds();
    var dateTime = date+' '+time;
    alert(dateTime);
}
</script>
</html>
like image 21
Braj Ankit Avatar answered Oct 25 '25 03:10

Braj Ankit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!