Reputation: 10934
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.
Upvotes: 4
Views: 11755
Reputation: 41
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
Upvotes: 3
Reputation: 538
You can do this using the momentJS, you can donwload the JS from below link : https://momentjs.com/
var moment = require('moment');
var momentDate = moment(date).format("ddd MMM DD YYYY HH:MM:ss:SSSSSSS Z");
Ti.API.info('momentDate ' + momentDate);
Hope this helps you better way, also have a look at the below link for more information about it. https://momentjs.com/docs/
Good Luck, Cheers
Ashish Sebastian
Upvotes: 1
Reputation: 9561
Try below example. For complete reference check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Conversion_getter
function addZero(v) {
return (v < 10 ? v = '0' + v : v);
}
function getFullDate(dt) {
var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var mons = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var w = days[dt.getDay()];
var M = mons[dt.getMonth()];
var d = addZero(dt.getDate());
var y = dt.getFullYear();
var h = addZero(dt.getHours());
var m = addZero(dt.getMinutes());
var s = addZero(dt.getSeconds());
var z = dt.getMilliseconds();
if (z < 10)
z = '00' + z;
else if (z < 100)
z = '0' + z;
var fulldate = w + " " + M + " " + d + " " + y + " " + h + ":" + m + ":" + s + ":" + z;
return fulldate;
}
console.log(getFullDate(new Date()));
Upvotes: 0
Reputation: 2249
Here you go:
var d = new Date();
var n = d.getMilliseconds();
If you want the full date:
var d = new Date();
var h = addZero(d.getHours(), 2);
var m = addZero(d.getMinutes(), 2);
var s = addZero(d.getSeconds(), 2);
var ms = addZero(d.getMilliseconds(), 3);
var fullDate = h + ":" + m + ":" + s + ":" + ms;
function addZero(x, n) {
while (x.toString().length < n) {
x = "0" + x;
}
return x;
}
Upvotes: 0
Reputation: 333
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>
Upvotes: 1
Reputation:
For microseconds, there is DOMHighResTimeStamp API ( https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp ) - try performance.now()
Upvotes: 0