Skip to content

Commit

Permalink
links and short explanation added for unary plus operator before date…
Browse files Browse the repository at this point in the history
… object
  • Loading branch information
nurrony committed Feb 20, 2016
1 parent e488011 commit 6183dd5
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions _posts/en/2016-02-15-extract-unix-timestamp-easily.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ We frequently need to calculate with unix timestamp. There are several ways to g
const timestamp = Date.now();
```
or

```js
const timestamp = new Date().getTime();
```
Expand All @@ -24,8 +25,7 @@ To get unix timestamp of a specific date pass `yyyy-mm-dd` as a parameter of `Da
```js
const timestamp = new Date('2012-06-08').getTime()
```

If you like magic, just add a `+` sign when declaring a `Date` object like below
You can just add a `+` sign also when declaring a `Date` object like below

```js
const timestamp = +new Date()
Expand All @@ -34,4 +34,10 @@ or for specific date

```js
const timestamp = +new Date('2012-06-08')
```
```
Under the hood the runtime calls `valueOf` method of the Date object. Then the unary `+` operator calls `toNumber()` with that returned value. For detailed explanation please check the following links

[Date.prototype.valueOf](http://es5.github.io/#x15.9.5.8)
[Unary + operator](http://es5.github.io/#x11.4.6)
[toNumber()](http://es5.github.io/#x9.3)

0 comments on commit 6183dd5

Please sign in to comment.