|
| 1 | +English | [简体中文](./README.zh-CN.md) |
1 | 2 | <p align="center"><a href="#" target="_blank" rel="noopener noreferrer"><img width="550" |
2 | 3 | src="https://user-images.githubusercontent.com/17680888/39081119-3057bbe2-456e-11e8-862c-646133ad4b43.png" |
3 | 4 | alt="Day.js"></a></p> |
|
19 | 20 |
|
20 | 21 | > 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. |
21 | 22 |
|
22 | | -- Familiar Moment.js API & patterns |
23 | | -- Immutable |
24 | | -- Chainable |
25 | | -- All browsers support |
| 23 | +```js |
| 24 | +dayjs().startOf('month').add(1, 'day').set('year', 2018).format('YYYY-MM-DD HH:mm:ss'); |
| 25 | +``` |
| 26 | + |
| 27 | +- 🕒 Familiar Moment.js API & patterns |
| 28 | +- 💪 Immutable |
| 29 | +- 🔥 Chainable |
| 30 | +- 📦 2kb mini library |
| 31 | +- 👫 All browsers support |
| 32 | +--- |
| 33 | + |
| 34 | +## Installation |
| 35 | + |
| 36 | +You have multiple ways of getting Day.js: |
| 37 | + |
| 38 | +- Via NPM: |
| 39 | +```console |
| 40 | + npm install dayjs --save |
| 41 | +``` |
| 42 | +```js |
| 43 | + var dayjs = require('dayjs'); |
| 44 | + dayjs().format(); |
| 45 | +``` |
| 46 | +- Via CDN: |
| 47 | +```html |
| 48 | + <!-- Latest compiled and minified JavaScript --> |
| 49 | + <script src="https://unpkg.com/dayjs"></script> |
| 50 | + <script> |
| 51 | + dayjs().format(); |
| 52 | + </script> |
| 53 | +``` |
| 54 | + |
| 55 | +- Via download and self-hosting: |
| 56 | + |
| 57 | +Just download the latest version of Day.js at [https://unpkg.com/dayjs](https://unpkg.com/dayjs) |
| 58 | + |
| 59 | +## Getting Started |
| 60 | +Instead of modifying the native `Date.prototype`, Day.js creates a wrapper for the Date object, called `Dayjs` object. |
| 61 | +`Dayjs` object is inmmutable, that is to say, all api operation will return a new `Dayjs` object. |
| 62 | + |
| 63 | +## API |
| 64 | + |
| 65 | +Api will always return a new `Dayjs` object if not specified. |
| 66 | + |
| 67 | +* [Parse](#parse) |
| 68 | + * [Now](#now) |
| 69 | + * [String](#string) |
| 70 | + * [Unix Timestamp (milliseconds)](#unix-timestamp-milliseconds) |
| 71 | + * [Date](#date) |
| 72 | + * [Clone](#clone) |
| 73 | + * [Validation](#validation) |
| 74 | +* [Get + Set](#get--set) |
| 75 | + * [Year](#year) |
| 76 | + * [Month](#month) |
| 77 | + * [Date of Month](#date-1) |
| 78 | + * [Hour](#hour) |
| 79 | + * [Minute](#minute) |
| 80 | + * [Second](#second) |
| 81 | + * [Millisecond](#millisecond) |
| 82 | + * [Set](#set) |
| 83 | +* [Manipulate](#manipulate) |
| 84 | + * [Add](#add) |
| 85 | + * [Subtract](#subtract) |
| 86 | + * [Start of Time](#start-of-time) |
| 87 | + * [End of Time](#end-of-time) |
| 88 | +* [Display](#display) |
| 89 | + * [Format](#format) |
| 90 | + * [Difference](#different) |
| 91 | + * [Unix Timestamp (milliseconds)](#unix-timestamp-milliseconds-1) |
| 92 | + * [Unix Timestamp (seconds)](#unix-timestamp-seconds) |
| 93 | + * [Days in Month](#days-in-month) |
| 94 | + * [As Javascript Date](#as-javascript-date) |
| 95 | + * [As Array](#as-array) |
| 96 | + * [As JSON](#as-json) |
| 97 | + * [As ISO 8601 String](#as-ios-8601-string) |
| 98 | + * [As Object](#as-object) |
| 99 | + * [As String](#as-string) |
| 100 | +* [Query](#query) |
| 101 | + * [Is Before](#is-before) |
| 102 | + * [Is Same](#is-same) |
| 103 | + * [Is After](#is-after) |
| 104 | + * [Is Leap Year](#is-leap-year) |
| 105 | + |
| 106 | +--- |
| 107 | +### Parse |
| 108 | +Simply call `dayjs()` with one of the supported input types. |
| 109 | +#### Now |
| 110 | +To get the current date and time, just call dayjs() with no parameters. |
| 111 | +```js |
| 112 | +dayjs(); |
| 113 | +``` |
| 114 | +### String |
| 115 | +Creating from a string matches [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format. |
| 116 | +```js |
| 117 | +dayjs(String); |
| 118 | +dayjs("1995-12-25"); |
| 119 | +``` |
| 120 | +### Unix Timestamp (milliseconds) |
| 121 | +Passing an integer value representing the number of milliseconds since the Unix Epoch (Jan 1 1970 12AM UTC). |
| 122 | +```js |
| 123 | +dayjs(Number); |
| 124 | +dayjs(1318781876406); |
| 125 | +``` |
| 126 | +### Date |
| 127 | +Passing a pre-existing native Javascript Date object. |
| 128 | +```js |
| 129 | +dayjs(Date); |
| 130 | +dayjs(new Date(2018, 8, 18)); |
| 131 | +``` |
| 132 | +### Clone |
| 133 | +All `Dayjs` are inmmutable. If you want a copy of the object, just call `.clone()`. |
| 134 | +Calling dayjs() on a `Dayjs` object will also clone it. |
| 135 | +```js |
| 136 | +dayjs(Dayjs); |
| 137 | +dayjs().clone(); |
| 138 | +``` |
| 139 | +### Validation |
| 140 | +- return Boolean |
| 141 | + |
| 142 | +Check whether the `Dayjs` object considers the date invalid. |
| 143 | +```js |
| 144 | +dayjs().isValid(); |
| 145 | +``` |
| 146 | +--- |
| 147 | +### Get + Set |
| 148 | +Get and set date. |
| 149 | +#### Year |
| 150 | +- return Number |
| 151 | + |
| 152 | +Get year. |
| 153 | +```js |
| 154 | +dayjs().year(); |
| 155 | +``` |
| 156 | +#### Month |
| 157 | +- return Number |
| 158 | + |
| 159 | +Get month. |
| 160 | +```js |
| 161 | +dayjs().month(); |
| 162 | +``` |
| 163 | +#### Date of Month |
| 164 | +- return Number |
| 165 | + |
| 166 | +Get day of the month. |
| 167 | +```js |
| 168 | +dayjs().date(); |
| 169 | +``` |
| 170 | +#### Hour |
| 171 | +- return Number |
| 172 | + |
| 173 | +Get hour. |
| 174 | +```js |
| 175 | +dayjs().hour(); |
| 176 | +``` |
| 177 | +#### Minute |
| 178 | +- return Number |
| 179 | + |
| 180 | +Get minute. |
| 181 | +```js |
| 182 | +dayjs().minute(); |
| 183 | +``` |
| 184 | +#### Second |
| 185 | +- return Number |
| 186 | + |
| 187 | +Get second. |
| 188 | +```js |
| 189 | +dayjs().second(); |
| 190 | +``` |
| 191 | +#### Millisecond |
| 192 | +- return Number |
| 193 | + |
| 194 | +Get millisecond. |
| 195 | +```js |
| 196 | +dayjs().millisecond(); |
| 197 | +``` |
| 198 | +#### Set |
| 199 | +Date setter. |
| 200 | +Units are case insensitive |
| 201 | +```js |
| 202 | +dayjs().set(unit : String, value : Int); |
| 203 | +dayjs().set('month', 3); // April |
| 204 | +moment().set('second', 30); |
| 205 | +``` |
| 206 | +--- |
| 207 | +### Manipulate |
| 208 | +Once you have a `Dayjs` object, you may want to manipulate it in some way like this: |
| 209 | +```js |
| 210 | +dayjs().startOf('month').add(1, 'day').subtract(1, 'year') |
| 211 | +``` |
| 212 | +#### Add |
| 213 | +Return a new `Dayjs` object by adding time. |
| 214 | +```js |
| 215 | +dayjs().add(value : Number, unit : String); |
| 216 | +dayjs().add(7, 'day'); |
| 217 | +``` |
| 218 | +#### Subtract |
| 219 | +Return a new `Dayjs` object by subtracting time. exactly the same as `dayjs#add`. |
| 220 | +```js |
| 221 | +dayjs().subtract(value : Number, unit : String); |
| 222 | +dayjs().subtract(7, 'year'); |
| 223 | +``` |
| 224 | +#### Start of Time |
| 225 | +Return a new `Dayjs` object by by setting it to the start of a unit of time. |
| 226 | +```js |
| 227 | +dayjs().startOf(unit : String); |
| 228 | +dayjs().startOf('year'); |
| 229 | +``` |
| 230 | +#### End of Time |
| 231 | +Return a new `Dayjs` object by by setting it to the end of a unit of time. |
| 232 | +```js |
| 233 | +dayjs().endOf(unit : String); |
| 234 | +dayjs().endOf('month'); |
| 235 | +``` |
| 236 | +--- |
| 237 | +### Display |
| 238 | +Once parsing and manipulation are done, you need some way to display the `Dayjs` object. |
| 239 | +#### Format |
| 240 | +- return String |
| 241 | + |
| 242 | +This is the most robust display option. It takes a string of tokens and replaces them with their corresponding values. |
| 243 | +```js |
| 244 | +dayjs().format(String); |
| 245 | +dayjs().format(); // "2014-09-08T08:02:17-05:00" (ISO 8601, no fractional seconds) |
| 246 | +dayjs().format("[YYYY] MM-DDTHH:mm:ssZ"); // "[2014] 09-08T08:02:17-05:00" |
| 247 | +``` |
| 248 | +#### Difference |
| 249 | +- return Number |
| 250 | + |
| 251 | +To get the difference of two `Dayjs` object in milliseconds or other unit. |
| 252 | +```js |
| 253 | +dayjs().diff(Dayjs, unit); |
| 254 | +dayjs().diff(dayjs(), 'years'); // 0 |
| 255 | +``` |
| 256 | +#### Unix Timestamp (milliseconds) |
| 257 | +- return Number |
| 258 | + |
| 259 | +Outputs the number of milliseconds since the Unix Epoch |
| 260 | +```js |
| 261 | +dayjs().valueOf(); |
| 262 | +``` |
| 263 | +#### Unix Timestamp (seconds) |
| 264 | +- return Number |
| 265 | + |
| 266 | +Outputs a Unix timestamp (the number of seconds since the Unix Epoch). |
| 267 | +```js |
| 268 | +dayjs().unix(); |
| 269 | +``` |
| 270 | +#### Days in Month |
| 271 | +- return Number |
| 272 | + |
| 273 | +Get the number of days in the current month. |
| 274 | +```js |
| 275 | +dayjs().daysInMonth(); |
| 276 | +``` |
| 277 | +#### As Javascript Date |
| 278 | +- return Javascript `Date` object |
| 279 | + |
| 280 | +Get copy of the native `Date` object from `Dayjs` object. |
| 281 | +```js |
| 282 | +dayjs().toDate(); |
| 283 | +``` |
| 284 | +#### As Array |
| 285 | +- return Array |
| 286 | + |
| 287 | +Return an array that mirrors the parameters from new Date(). |
| 288 | +```js |
| 289 | +dayjs().toArray(); //[2018, 8, 18, 00, 00, 00, 000]; |
| 290 | +``` |
| 291 | +#### As JSON |
| 292 | +- return JSON String |
| 293 | + |
| 294 | +Serializing an `Dayjs` to JSON, will return an ISO8601 string. |
| 295 | +```js |
| 296 | +dayjs().toJSON(); //"2018-08-08T00:00:00.000Z" |
| 297 | +``` |
| 298 | +#### As ISO 8601 String |
| 299 | +- return String |
| 300 | + |
| 301 | +Formats a string to the ISO8601 standard. |
| 302 | +```js |
| 303 | +dayjs().toISOString(); |
| 304 | +``` |
| 305 | +#### As Object |
| 306 | +- return Object |
| 307 | + |
| 308 | +Return an object with year, month ... missisecond. |
| 309 | +```js |
| 310 | +dayjs().toObject();// { years:2018, months:8, date:18, hours:0, minutes:0, seconds:0, milliseconds:0} |
| 311 | +``` |
| 312 | +#### As String |
| 313 | +- return String |
| 314 | + |
| 315 | +```js |
| 316 | +dayjs().toString(); |
| 317 | +``` |
| 318 | +--- |
| 319 | +### Query |
| 320 | +#### Is Before |
| 321 | +- return Boolean |
| 322 | + |
| 323 | +Check if a `Dayjs` object is before another `Dayjs` object. |
| 324 | +```js |
| 325 | +dayjs().isBefore(Dayjs); |
| 326 | +dayjs().isBefore(dayjs()); // false |
| 327 | +``` |
| 328 | +#### Is Same |
| 329 | +- return Boolean |
| 330 | + |
| 331 | +Check if a `Dayjs` object is same as another `Dayjs` object. |
| 332 | +```js |
| 333 | +dayjs().isSame(Dayjs); |
| 334 | +dayjs().isSame(dayjs()); // true |
| 335 | +``` |
| 336 | +#### Is After |
| 337 | +- return Boolean |
| 338 | + |
| 339 | +Check if a `Dayjs` object is after another `Dayjs` object. |
| 340 | +```js |
| 341 | +dayjs().isAfter(Dayjs); |
| 342 | +dayjs().isAfter(dayjs()); // false |
| 343 | +``` |
| 344 | +#### Is Leap Year |
| 345 | +- return Boolean |
| 346 | + |
| 347 | +Check if a year is a leap year. |
| 348 | +```js |
| 349 | +dayjs().isLeapYear(); |
| 350 | +dayjs('2000-01-01').isLeapYear(dayjs()); // true |
| 351 | +``` |
26 | 352 | --- |
27 | 353 | ## License |
28 | 354 |
|
29 | | -MIT |
| 355 | +MIT |
0 commit comments