|
|
|
|
|
by biturd
4061 days ago
|
|
I was looking at some of the code for one of his projects that is a little node http server. I don't really know JS or node at all, but can someone explain this to me: function secondsSince (when) {
var now = new Date ();
when = new Date (when);
return ((now - when) / 1000);
}
I'm assuming now returns seconds since some fixed point in time ( epoch ) when when someone passes in the 'when' argument, it must already be formatted a certain way, or node/js somehow managed to figure out the input? How would it deal with 5/8/2015 vs 8/5/2015Or this is just a very case specific function and the input is already sanitized in a way that is prepared for this function? github is here: https://github.com/scripting/pagepark/blob/master/lib/utils.... |
|
When coerced to a number, it's the number of milliseconds since January 1, 1970.
If you wanted to know the number of seconds since January 1, 1970, you could divide by 1000. Since JavaScript will do the coercion for you automatically you can pretty much treat a date object as if it were a number.
This is basically a utility routine that I use for debugging messages to say how long an operation took.
Here's an example of its use:
https://github.com/scripting/myWordEditor/blob/master/script...
It's especially nice because JavaScript automatically does real arithmetic on the division, so you get fractions of seconds displayed in the console.
I use this routine a lot. Helps me spot performance problems. Nice to have, not a crucial routine.
Hope this helps! ;-)