Skip to content

Commit

Permalink
Merge pull request #349 from iamkun/feature/iamkun
Browse files Browse the repository at this point in the history
add .unix API
  • Loading branch information
iamkun authored Sep 25, 2018
2 parents 39b1e0a + 476b59b commit a767a55
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 23 deletions.
20 changes: 16 additions & 4 deletions docs/en/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)`
Expand Down
20 changes: 14 additions & 6 deletions docs/ja/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) 以降のミリ秒数を表す整数値を渡します。
Expand All @@ -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
Expand Down
20 changes: 16 additions & 4 deletions docs/ko/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)`
Expand Down
18 changes: 14 additions & 4 deletions docs/pt-br/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)`
Expand Down
20 changes: 15 additions & 5 deletions docs/zh-cn/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
* [解析](#解析)
* [当前时间](#当前时间)
* [时间字符串](#时间字符串)
* [Unix 时间戳 (毫秒)](#unix-时间戳-毫秒)
* [Date 对象](#date-对象)
* [Unix 时间戳 (毫秒)](#unix-时间戳-毫秒)
* [Unix 时间戳 (秒)](#unix-时间戳-秒)
* [复制](#复制)
* [验证](#验证)
* [获取+设置](#获取设置)
Expand Down Expand Up @@ -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` 对象也能实现同样的效果。
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,10 @@ dayjs.locale = parseLocale

dayjs.isDayjs = isDayjs

dayjs.unix = timestamp => (
dayjs(timestamp * 1e3)
)

dayjs.en = Ls[L]

export default dayjs
7 changes: 7 additions & 0 deletions test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit a767a55

Please sign in to comment.