13
<script language="JavaScript">
var t = new Date();
t.getTime() + -864e5;
</script>

What is that funky code after the "+" at the end of the second line doing?

It is probably made to be hard to understand, since I suspect it's one of the ways they try to protect themselves against scraping.

vinayakj
  • 5,591
  • 3
  • 28
  • 48
Mattis
  • 5,026
  • 3
  • 34
  • 51

4 Answers4

25

It is a valid JavaScript number that represents the number of milliseconds in a 24 hour day.

1000*60*60*24 or 86400000 or 864e5
Simon Steinberger
  • 6,605
  • 5
  • 55
  • 97
cocco
  • 16,442
  • 7
  • 62
  • 77
  • 4
    No idea why anyone would want to use this method - it really makes the code much harder to read... So hard even that you might need to post a question on [so] to understand it :P – Lix Aug 21 '13 at 13:58
  • @Mattis: It negates the number. Like `5` and `-5`. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#-_.28Unary_Negation.29 . Of course it would be easier to just write `a - b` instead of `a + (-b)` but well... – Felix Kling Aug 21 '13 at 13:58
  • @Mattis: No, it's just to make the script shorter. It doesn't really complicate scraping, every floating point number parser supports this format. – Bergi Aug 21 '13 at 14:04
4

-864e5 means "minus 1 day". So the JavaScript is really getting the date/time 24 hours ago.

mynetx
  • 878
  • 7
  • 25
2

864e5 is a valid JavaScript number that represents the number of milliseconds (a millisecond is 1/1000'th of a second) in a 24 hour day.

1000*60*60*24 = 86400000 or using exponential notation 864e5

e3495026
  • 79
  • 1
  • 7
1

It looks like the + -864e5 is offsetting the time 1 day into the past.

Its true its not very readable, or makes much sense to people looking at it for a first time, but there isn't really any other way in bare js (at this point).

Shade
  • 775
  • 3
  • 16
  • `getTime()` returns milliseconds. – mynetx Aug 21 '13 at 14:02
  • bummer. I was hoping the math above in cocco's anwser was resulting in days. post edited. – Shade Aug 21 '13 at 14:04
  • Sure there's other ways. You could write out the number in regular notation if you're worried about people not understanding if you use scientific. Or if you're worried about people not knowing the significance of `86400000`, you could put a comment explaining that. And why use `+ -864e5` and not just `- 864e5`? – flarn2006 Aug 01 '17 at 15:45