Skip to content

Commit

Permalink
Merge pull request #29 from xx45/dev
Browse files Browse the repository at this point in the history
fix(fix): readme api
  • Loading branch information
iamkun authored Apr 23, 2018
2 parents 50d99af + 38be882 commit 9734fd8
Showing 1 changed file with 331 additions and 5 deletions.
336 changes: 331 additions & 5 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
English | [简体中文](./README.zh-CN.md)
<p align="center"><a href="#" target="_blank" rel="noopener noreferrer"><img width="550"
src="https://user-images.githubusercontent.com/17680888/39081119-3057bbe2-456e-11e8-862c-646133ad4b43.png"
alt="Day.js"></a></p>
Expand All @@ -19,11 +20,336 @@

> Day.js is a minimalist JavaScript library for modern browsers with a largely Moment.js-compatible API. If you use Moment.js, you already know how to use Day.js.
- Familiar Moment.js API & patterns
- Immutable
- Chainable
- All browsers support
```js
dayjs().startOf('month').add(1, 'day').set('year', 2018).format('YYYY-MM-DD HH:mm:ss');
```

- 🕒 Familiar Moment.js API & patterns
- 💪 Immutable
- 🔥 Chainable
- 📦 2kb mini library
- 👫 All browsers support
---

## Installation

You have multiple ways of getting Day.js:

- Via NPM:
```console
npm install dayjs --save
```
```js
var dayjs = require('dayjs');
dayjs().format();
```
- Via CDN:
```html
<!-- Latest compiled and minified JavaScript -->
<script src="https://unpkg.com/dayjs"></script>
<script>
dayjs().format();
</script>
```

- Via download and self-hosting:

Just download the latest version of Day.js at [https://unpkg.com/dayjs](https://unpkg.com/dayjs)

## Getting Started
Instead of modifying the native `Date.prototype`, Day.js creates a wrapper for the Date object, called `Dayjs` object.
`Dayjs` object is inmmutable, that is to say, all api operation will return a new `Dayjs` object.

## API

Api will always return a new `Dayjs` object if not specified.

* [Parse](#parse)
* [Now](#now)
* [String](#string)
* [Unix Timestamp (milliseconds)](#unix-timestamp-milliseconds)
* [Date](#date)
* [Clone](#clone)
* [Validation](#validation)
* [Get + Set](#get--set)
* [Year](#year)
* [Month](#month)
* [Date of Month](#date-1)
* [Hour](#hour)
* [Minute](#minute)
* [Second](#second)
* [Millisecond](#millisecond)
* [Set](#set)
* [Manipulate](#manipulate)
* [Add](#add)
* [Subtract](#subtract)
* [Start of Time](#start-of-time)
* [End of Time](#end-of-time)
* [Display](#display)
* [Format](#format)
* [Difference](#different)
* [Unix Timestamp (milliseconds)](#unix-timestamp-milliseconds-1)
* [Unix Timestamp (seconds)](#unix-timestamp-seconds)
* [Days in Month](#days-in-month)
* [As Javascript Date](#as-javascript-date)
* [As Array](#as-array)
* [As JSON](#as-json)
* [As ISO 8601 String](#as-ios-8601-string)
* [As Object](#as-object)
* [As String](#as-string)
* [Query](#query)
* [Is Before](#is-before)
* [Is Same](#is-same)
* [Is After](#is-after)
* [Is Leap Year](#is-leap-year)

---
### Parse
Simply call `dayjs()` with one of the supported input types.
#### Now
To get the current date and time, just call dayjs() with no parameters.
```js
dayjs();
```
### String
Creating from a string matches [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format.
```js
dayjs(String);
dayjs("1995-12-25");
```
### Unix Timestamp (milliseconds)
Passing an integer value representing the number of milliseconds since the Unix Epoch (Jan 1 1970 12AM UTC).
```js
dayjs(Number);
dayjs(1318781876406);
```
### Date
Passing a pre-existing native Javascript Date object.
```js
dayjs(Date);
dayjs(new Date(2018, 8, 18));
```
### Clone
All `Dayjs` are inmmutable. If you want a copy of the object, just call `.clone()`.
Calling dayjs() on a `Dayjs` object will also clone it.
```js
dayjs(Dayjs);
dayjs().clone();
```
### Validation
- return Boolean

Check whether the `Dayjs` object considers the date invalid.
```js
dayjs().isValid();
```
---
### Get + Set
Get and set date.
#### Year
- return Number

Get year.
```js
dayjs().year();
```
#### Month
- return Number

Get month.
```js
dayjs().month();
```
#### Date of Month
- return Number

Get day of the month.
```js
dayjs().date();
```
#### Hour
- return Number

Get hour.
```js
dayjs().hour();
```
#### Minute
- return Number

Get minute.
```js
dayjs().minute();
```
#### Second
- return Number

Get second.
```js
dayjs().second();
```
#### Millisecond
- return Number

Get millisecond.
```js
dayjs().millisecond();
```
#### Set
Date setter.
Units are case insensitive
```js
dayjs().set(unit : String, value : Int);
dayjs().set('month', 3); // April
moment().set('second', 30);
```
---
### Manipulate
Once you have a `Dayjs` object, you may want to manipulate it in some way like this:
```js
dayjs().startOf('month').add(1, 'day').subtract(1, 'year')
```
#### Add
Return a new `Dayjs` object by adding time.
```js
dayjs().add(value : Number, unit : String);
dayjs().add(7, 'day');
```
#### Subtract
Return a new `Dayjs` object by subtracting time. exactly the same as `dayjs#add`.
```js
dayjs().subtract(value : Number, unit : String);
dayjs().subtract(7, 'year');
```
#### Start of Time
Return a new `Dayjs` object by by setting it to the start of a unit of time.
```js
dayjs().startOf(unit : String);
dayjs().startOf('year');
```
#### End of Time
Return a new `Dayjs` object by by setting it to the end of a unit of time.
```js
dayjs().endOf(unit : String);
dayjs().endOf('month');
```
---
### Display
Once parsing and manipulation are done, you need some way to display the `Dayjs` object.
#### Format
- return String

This is the most robust display option. It takes a string of tokens and replaces them with their corresponding values.
```js
dayjs().format(String);
dayjs().format(); // "2014-09-08T08:02:17-05:00" (ISO 8601, no fractional seconds)
dayjs().format("[YYYY] MM-DDTHH:mm:ssZ"); // "[2014] 09-08T08:02:17-05:00"
```
#### Difference
- return Number

To get the difference of two `Dayjs` object in milliseconds or other unit.
```js
dayjs().diff(Dayjs, unit);
dayjs().diff(dayjs(), 'years'); // 0
```
#### Unix Timestamp (milliseconds)
- return Number

Outputs the number of milliseconds since the Unix Epoch
```js
dayjs().valueOf();
```
#### Unix Timestamp (seconds)
- return Number

Outputs a Unix timestamp (the number of seconds since the Unix Epoch).
```js
dayjs().unix();
```
#### Days in Month
- return Number

Get the number of days in the current month.
```js
dayjs().daysInMonth();
```
#### As Javascript Date
- return Javascript `Date` object

Get copy of the native `Date` object from `Dayjs` object.
```js
dayjs().toDate();
```
#### As Array
- return Array

Return an array that mirrors the parameters from new Date().
```js
dayjs().toArray(); //[2018, 8, 18, 00, 00, 00, 000];
```
#### As JSON
- return JSON String

Serializing an `Dayjs` to JSON, will return an ISO8601 string.
```js
dayjs().toJSON(); //"2018-08-08T00:00:00.000Z"
```
#### As ISO 8601 String
- return String

Formats a string to the ISO8601 standard.
```js
dayjs().toISOString();
```
#### As Object
- return Object

Return an object with year, month ... missisecond.
```js
dayjs().toObject();// { years:2018, months:8, date:18, hours:0, minutes:0, seconds:0, milliseconds:0}
```
#### As String
- return String

```js
dayjs().toString();
```
---
### Query
#### Is Before
- return Boolean

Check if a `Dayjs` object is before another `Dayjs` object.
```js
dayjs().isBefore(Dayjs);
dayjs().isBefore(dayjs()); // false
```
#### Is Same
- return Boolean

Check if a `Dayjs` object is same as another `Dayjs` object.
```js
dayjs().isSame(Dayjs);
dayjs().isSame(dayjs()); // true
```
#### Is After
- return Boolean

Check if a `Dayjs` object is after another `Dayjs` object.
```js
dayjs().isAfter(Dayjs);
dayjs().isAfter(dayjs()); // false
```
#### Is Leap Year
- return Boolean

Check if a year is a leap year.
```js
dayjs().isLeapYear();
dayjs('2000-01-01').isLeapYear(dayjs()); // true
```
---
## License

MIT
MIT

0 comments on commit 9734fd8

Please sign in to comment.