Repository: iamkun/dayjs
Pull request #349 (2018-09-25 08:53:52)

add .unix API


Patch:
diff --git a/docs/en/API-reference.md b/docs/en/API-reference.md
index c17e844..188b7dd 100644
--- a/docs/en/API-reference.md
+++ b/docs/en/API-reference.md
@@ -8,8 +8,9 @@ The `Dayjs` object is immutable, that is, all API operations that change the `Da
   - [Parsing](#parsing)
     - [Constructor `dayjs(existing?: string | number | Date | Dayjs)`](#constructor-dayjsexisting-string--number--date--dayjs)
       - [ISO 8601 string](#iso-8601-string)
-      - [Unix Timestamp (milliseconds since the Unix Epoch - Jan 1 1970, 12AM UTC)](#unix-timestamp-milliseconds-since-the-unix-epoch---jan-1-1970-12am-utc)
       - [Native Javascript Date object](#native-javascript-date-object)
+      - [Unix Timestamp (milliseconds)](#unix-timestamp-milliseconds)
+    - [Unix Timestamp (seconds)](#unix-timestamp-seconds-unixvalue-number)
     - [Clone `.clone() | dayjs(original: Dayjs)`](#clone-clone--dayjsoriginal-dayjs)
     - [Validation `.isValid()`](#validation-isvalid)
   - [Get and Set](#get-and-set)
@@ -70,16 +71,27 @@ Day.js also parses other date formats.
 dayjs('2018-04-04T16:00:00.000Z');
 ```
 
-#### Unix Timestamp (milliseconds since the Unix Epoch - Jan 1 1970, 12AM UTC)
+#### Native Javascript Date object
+
+```js
+dayjs(new Date(2018, 8, 18));
+```
+
+#### Unix Timestamp (milliseconds)
+
+Returns a `Dayjs` from a Unix timestamp (milliseconds since the Unix Epoch)
 
 ```js
 dayjs(1318781876406);
 ```
 
-#### Native Javascript Date object
+### Unix Timestamp (seconds) `.unix(value: number)`
+
+Returns a `Dayjs` from a Unix timestamp (seconds since the Unix Epoch)
 
 ```js
-dayjs(new Date(2018, 8, 18));
+dayjs.unix(1318781876);
+dayjs.unix(1318781876.721);
 ```
 
 ### Clone `.clone() | dayjs(original: Dayjs)`
diff --git a/docs/ja/API-reference.md b/docs/ja/API-reference.md
index e66825f..1287727 100644
--- a/docs/ja/API-reference.md
+++ b/docs/ja/API-reference.md
@@ -7,8 +7,9 @@ Day.js は組み込みの `Date.prototype` を変更する代わりに `Dayjs` 
 * [Parse](#parse)
   * [Now](#now)
   * [String](#string)
-  * [Unix Timestamp (milliseconds)](#unix-timestamp-milliseconds)
   * [Date](#date)
+  * [Unix Timestamp (milliseconds)](#unix-timestamp-milliseconds)
+  * [Unix Timestamp (seconds)](#unix-timestamp-seconds)
   * [Clone](#clone)
   * [Validation](#validation)
 * [Get + Set](#get--set)
@@ -74,6 +75,15 @@ dayjs(String);
 dayjs('1995-12-25');
 ```
 
+### Date
+
+JavaScript の組み込みの Date オブジェクトを渡します。
+
+```js
+dayjs(Date);
+dayjs(new Date(2018, 8, 18));
+```
+
 ### Unix Timestamp (milliseconds)
 
 Unix エポック (1970年1月1日 12:00AM UTC) 以降のミリ秒数を表す整数値を渡します。
@@ -83,13 +93,11 @@ dayjs(Number);
 dayjs(1318781876406);
 ```
 
-### Date
-
-JavaScript の組み込みの Date オブジェクトを渡します。
+### Unix Timestamp (seconds)
 
 ```js
-dayjs(Date);
-dayjs(new Date(2018, 8, 18));
+dayjs.unix(Number);
+dayjs.unix(1318781876);
 ```
 
 ### Clone
diff --git a/docs/ko/API-reference.md b/docs/ko/API-reference.md
index 82d2ce3..d7286de 100644
--- a/docs/ko/API-reference.md
+++ b/docs/ko/API-reference.md
@@ -8,8 +8,9 @@ Day.js는 네이티브 `Date.prototype`을 수정하는 대신 `Dayjs` 오브젝
   - [Parsing](#parsing)
     - [Constructor `dayjs(existing?: string | number | Date | Dayjs)`](#constructor-dayjsexisting--string-number-date-dayjs)
       - [ISO 8601 string](#iso-8601https---enwikipediaorg-wiki-iso-8601-string)
-      - [Unix Timestamp (milliseconds since the Unix Epoch - Jan 1 1970, 12AM UTC)](#unix-timestamp-milliseconds-since-the-unix-epoch---jan-1-1970--12am-utc)
       - [Native Javascript Date object](#native-javascript-date-object)
+      - [Unix Timestamp (milliseconds)](#unix-timestamp-milliseconds)
+    - [Unix Timestamp (seconds)](#unix-timestamp-seconds-unixvalue-number)
     - [Clone `.clone() | dayjs(original: Dayjs)`](#clone-clone-dayjsoriginal--dayjs)
     - [Validation `.isValid()`](#validation-isvalid)
   - [Get and Set](#get-and-set)
@@ -69,16 +70,27 @@ Day.js는 다른 날짜 형식도 구분 분석합니다.
 dayjs('2018-04-04T16:00:00.000Z');
 ```
 
-#### Unix Timestamp (milliseconds since the Unix Epoch - Jan 1 1970, 12AM UTC)
+#### Native Javascript Date object
+
+```js
+dayjs(new Date(2018, 8, 18));
+```
+
+#### Unix Timestamp (milliseconds)
+
+Returns a `Dayjs` from a Unix timestamp (milliseconds since the Unix Epoch)
 
 ```js
 dayjs(1318781876406);
 ```
 
-#### Native Javascript Date object
+### Unix Timestamp (seconds) `.unix(value: number)`
+
+Returns a `Dayjs` from a Unix timestamp (seconds since the Unix Epoch)
 
 ```js
-dayjs(new Date(2018, 8, 18));
+dayjs.unix(1318781876);
+dayjs.unix(1318781876.721);
 ```
 
 ### Clone `.clone() | dayjs(original: Dayjs)`
diff --git a/docs/pt-br/API-reference.md b/docs/pt-br/API-reference.md
index 98e27ab..7d12d63 100644
--- a/docs/pt-br/API-reference.md
+++ b/docs/pt-br/API-reference.md
@@ -8,8 +8,9 @@ O objeto `Dayjs` é imutável, ou seja, todas as operações da API que alteram
   - [Conversões](#conversões)
     - [Construtor `dayjs(existing?: string | number | Date | Dayjs)`](#construtor-dayjsexisting-string--number--date--dayjs)
       - [string ISO 8601](#string-iso-8601)
-      - [Unix Timestamp (milissegundos desde a Unix Epoch - Jan 1 1970, 12AM UTC)](#unix-timestamp-milissegundos-desde-a-unix-epoch---jan-1-1970-12am-utc)
       - [Objeto `Date` nativo](#objeto-date-nativo)
+      - [Unix Timestamp (milliseconds)](#unix-timestamp-milliseconds)
+    - [Unix Timestamp (seconds)](#unix-timestamp-seconds-unixvalue-number)
     - [Clonar `.clone() | dayjs(original: Dayjs)`](#clonar-clone--dayjsoriginal-dayjs)
     - [Validação `.isValid()`](#validação-isvalid)
   - [Get and Set](#get-and-set)
@@ -70,16 +71,25 @@ Day.js também converte outros formatos de data.
 dayjs('2018-04-04T16:00:00.000Z');
 ```
 
-#### Unix Timestamp (milissegundos desde a Unix Epoch - Jan 1 1970, 12AM UTC)
+#### Objeto `Date` nativo
+
+```js
+dayjs(new Date(2018, 8, 18));
+```
+
+#### Unix Timestamp (milliseconds)
 
 ```js
 dayjs(1318781876406);
 ```
 
-#### Objeto `Date` nativo
+### Unix Timestamp (seconds) `.unix(value: number)`
+
+Returns a `Dayjs` from a Unix timestamp (seconds since the Unix Epoch)
 
 ```js
-dayjs(new Date(2018, 8, 18));
+dayjs.unix(1318781876);
+dayjs.unix(1318781876.721);
 ```
 
 ### Clonar `.clone() | dayjs(original: Dayjs)`
diff --git a/docs/zh-cn/API-reference.md b/docs/zh-cn/API-reference.md
index 48340eb..a75582c 100644
--- a/docs/zh-cn/API-reference.md
+++ b/docs/zh-cn/API-reference.md
@@ -7,8 +7,9 @@
 * [解析](#解析)
   * [当前时间](#当前时间)
   * [时间字符串](#时间字符串)
-  * [Unix 时间戳 (毫秒)](#unix-时间戳-毫秒)
   * [Date 对象](#date-对象)
+  * [Unix 时间戳 (毫秒)](#unix-时间戳-毫秒)
+  * [Unix 时间戳 (秒)](#unix-时间戳-秒)
   * [复制](#复制)
   * [验证](#验证)
 * [获取+设置](#获取设置)
@@ -65,18 +66,27 @@ dayjs();
 dayjs(String);
 dayjs("1995-12-25");
 ```
+### Date 对象
+可以解析传入的一个 Javascript Date 对象。
+```js
+dayjs(Date);
+dayjs(new Date(2018, 8, 18));
+```
+
 ### Unix 时间戳 (毫秒)
 可以解析传入的一个 Unix 时间戳 (13位数字)。
 ```js
 dayjs(Number);
 dayjs(1318781876406);
 ```
-### Date 对象
-可以解析传入的一个 Javascript Date 对象。
+
+### Unix 时间戳 (秒)
+可以解析传入的一个 Unix 时间戳 (10位数字)。
 ```js
-dayjs(Date);
-dayjs(new Date(2018, 8, 18));
+dayjs.unix(Number);
+dayjs.unix(1318781876);
 ```
+
 ### 复制
 `Dayjs` 对象是不可变的，如果您想获得一个对象的拷贝，请执行 `.clone()`。
 向 `dayjs()` 里传入一个 `Dayjs` 对象也能实现同样的效果。
diff --git a/src/index.js b/src/index.js
index e34a920..7b37258 100644
--- a/src/index.js
+++ b/src/index.js
@@ -446,6 +446,10 @@ dayjs.locale = parseLocale
 
 dayjs.isDayjs = isDayjs
 
+dayjs.unix = timestamp => (
+  dayjs(timestamp * 1e3)
+)
+
 dayjs.en = Ls[L]
 
 export default dayjs


Test patch:
diff --git a/test/parse.test.js b/test/parse.test.js
index 395d368..5dab174 100644
--- a/test/parse.test.js
+++ b/test/parse.test.js
@@ -56,6 +56,13 @@ it('Unix Timestamp Number (milliseconds) 1523520536000', () => {
   expect(dayjs(timestamp).valueOf()).toBe(moment(timestamp).valueOf())
 })
 
+it('Unix Timestamp Number (seconds) 1318781876', () => {
+  const timestamp1 = 1318781876
+  const timestamp2 = 1318781876.721
+  expect(dayjs.unix(timestamp1).valueOf()).toBe(moment.unix(timestamp1).valueOf())
+  expect(dayjs.unix(timestamp2).valueOf()).toBe(moment.unix(timestamp2).valueOf())
+})
+
 it('String and Number 20180101', () => {
   expect(dayjs(20180101).valueOf()).toBe(moment(20180101).valueOf())
   expect(dayjs('20180101').valueOf()).toBe(moment('20180101').valueOf())