8

In PHP microtime() return the string in "microsec sec".

php microtime()

eg: '0.48445100 1470284726'

In JavaScript there is no default function for microtime().
So divided the return type, where sec is in unix timestamp using date.getTime() returned the sec value, eg:

var date  = new Date();
var timestamp = Math.round(date.getTime()/1000 | 0); 

Then how to get the 'microsec' value in JavaScript.

Flygenring
  • 3,818
  • 1
  • 32
  • 39
Harish98
  • 445
  • 1
  • 7
  • 16
  • 2
    Possible duplicate of [Does JavaScript provide a high resolution timer?](http://stackoverflow.com/questions/6875625/does-javascript-provide-a-high-resolution-timer) – JSchirrmacher Aug 04 '16 at 04:58
  • Subtract a rounded date from a date? – zerkms Aug 04 '16 at 06:55
  • What do you really want to use this for? JS gives you millisecond resolution timestamps. That's not microseconds resolution, so dividing it up in some way isn't going to get you that. What's the use case? – deceze Aug 04 '16 at 07:09
  • purpose need to create oauth_nonce to generate url. but in PHP there is default function uses the microtime(), getting confuse what method to implement to replace microtime in javascript. – Harish98 Aug 04 '16 at 07:29
  • So what you really need is just a *random number*? – deceze Aug 04 '16 at 07:39
  • @deceze nonce tokens aren't random numbers. The timestamp needs to be close enough to `now` for the request to succeed. This prevents replay attacks. See also https://stackoverflow.com/a/23090269/1979230 – Brian Hannay May 23 '18 at 04:24
  • @BrianHannay A nonce must contain a random component; if it’s just the current timestamp, it’s utterly *predictable*. – deceze May 23 '18 at 05:14

1 Answers1

23

In PHP microtime actually gives you the fraction of a second with microsecond resolution.

JavaScript uses milliseconds and not seconds for its timestamps, so you can only get the fraction part with millisecond resolution.

But to get this, you would just take the timestamp, divide it by 1000 and get the remainder, like this:

var microtime = (Date.now() % 1000) / 1000;

For a more complete implementation of the PHP functionality, you can do something like this (shortened from PHP.js):

function microtime(getAsFloat) {
    var s,
        now = (Date.now ? Date.now() : new Date().getTime()) / 1000;

    // Getting microtime as a float is easy
    if(getAsFloat) {
        return now
    }

    // Dirty trick to only get the integer part
    s = now | 0

    return (Math.round((now - s) * 1000) / 1000) + ' ' + s
}

EDIT : Using the newer High Resolution Time API it's possible to get microsecond resolutions in most modern browsers

function microtime(getAsFloat) {
    var s, now, multiplier;

    if(typeof performance !== 'undefined' && performance.now) {
        now = (performance.now() + performance.timing.navigationStart) / 1000;
        multiplier = 1e6; // 1,000,000 for microseconds
    }
    else {
        now = (Date.now ? Date.now() : new Date().getTime()) / 1000;
        multiplier = 1e3; // 1,000
    }

    // Getting microtime as a float is easy
    if(getAsFloat) {
        return now;
    }

    // Dirty trick to only get the integer part
    s = now | 0;

    return (Math.round((now - s) * multiplier ) / multiplier ) + ' ' + s;
}
Flygenring
  • 3,818
  • 1
  • 32
  • 39
  • If you're using this to get a seemingly random number, as asked in the comments to the question, would advice against using the method that produces microseconds, as it doesn't seem to do too good a job at that yet! – Flygenring Aug 04 '16 at 21:46