From 6183dd5093f86d16d09d15f8515cddd2e5d1965d Mon Sep 17 00:00:00 2001 From: Nur Rony Date: Sun, 21 Feb 2016 01:22:16 +0600 Subject: [PATCH] links and short explanation added for unary plus operator before date object --- .../en/2016-02-15-extract-unix-timestamp-easily.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/_posts/en/2016-02-15-extract-unix-timestamp-easily.md b/_posts/en/2016-02-15-extract-unix-timestamp-easily.md index 613d628d..3662ded8 100644 --- a/_posts/en/2016-02-15-extract-unix-timestamp-easily.md +++ b/_posts/en/2016-02-15-extract-unix-timestamp-easily.md @@ -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(); ``` @@ -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() @@ -34,4 +34,10 @@ or for specific date ```js const timestamp = +new Date('2012-06-08') -``` \ No newline at end of file +``` +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) +