Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get ten millionths of a second in JavaScript? "fffffff"

Tags:

javascript

I need a date format like this: yyyy-MM-dd_HH.mm.ss.fffffff

And I tried this:

var currentdate = new Date();
var fileName = currentdate.getFullYear() + '-'
   + currentdate.getMonth() + '-'
   + currentdate.getDate() + '_'
   + currentdate.getHours() + '.'
   + currentdate.getMinutes() + '.'
   + currentdate.getSeconds() + '.'
   + currentdate.getMilliseconds();

And output is this: 2014-3-2_23.0.29.840

How can I get ten millionths of a second so my output will be something like following?

2014-3-2_23.0.29.8401111

For some reasons I can't use libraries such as moment.js.

Thanks.

like image 688
Zafar Avatar asked Oct 02 '22 03:10

Zafar


1 Answers

Date object doesn't manage accuracy beyond milliseconds, however, recently the browsers have begun to implement the method performance.now(), that returns " a DOMHighResTimeStamp, measured in milliseconds, accurate to one thousandth of a millisecond equal to the number of milliseconds since the PerformanceTiming.navigationStart property"

There is an interesting post here about that.

I've modified the now() function included in the post in order to work similar to Date.now() but with more precision: http://jsfiddle.net/aTpD5/3/

As summary, this is the code to achieve it:

var now = (function() {

  // Returns the number of milliseconds elapsed since either the browser navigationStart event or 
  // the UNIX epoch, depending on availability.
  // Where the browser supports 'performance' we use that as it is more accurate (microsoeconds
  // will be returned in the fractional part) and more reliable as it does not rely on the system time. 
  // Where 'performance' is not available, we will fall back to Date().getTime().

  var performance = window.performance || {};

  performance.now = (function() {
    return performance.now    ||
    performance.webkitNow     ||
    performance.msNow         ||
    performance.oNow          ||
    performance.mozNow        ||
    function() { return new Date().getTime(); };
  })();

  if (performance.timing) {
        return performance.timing.navigationStart + performance.now();
  }

  return performance.now();         

});

The now() function returns milliseconds with fractions of milliseconds, something like: "1396464605263.821ms" To format it you can use following code:

var exactNow = now();
$('#output').text(exactNow + 'ms')
var isostr = new Date(exactNow).toISOString().replace(/T/, " ");
// You can increase the precision changing the 6 by a higher number
var fractionms = ('' + (exactNow % 1)).substring(2, 6);
isostr = isostr.substring(0, isostr.length - 1) + fractionms;
$('#output2').text(isostr)

Previous code will show something like:

1396465845046.267ms
2014-04-02 19:10:45.0462670

This feature doesn't work on Safari, but It should work on other modern browsers:

  • Chrome: 20+ (webkit)
  • Firefox: 15.0+ (Gecko)
  • Internet Explorer: 10.0+
  • Opera: 15.0+
  • Safari: Not supported (Tested on 7.0.3 in OSX)
like image 53
Roberto Avatar answered Oct 05 '22 11:10

Roberto