OSDN Git Service

Regular updates
[twpd/master.git] / moment.md
1 ---
2 title: Moment.js
3 category: JavaScript libraries
4 layout: 2017/sheet
5 updated: 2018-09-15
6 tags: [Featurable]
7 ---
8
9 ### Parsing
10
11 ```js
12 m = moment('2013-03-01', 'YYYY-MM-DD')
13 ```
14
15 This parses the given date using the given format. Returns a moment object.
16
17 ### Formatting
18
19 ```js
20 m.format()            // "2013-03-01T00:00:00+01:00"
21 m.format('dddd')      // "Friday"
22 m.format('MMM Do YY') // "Mar 1st 13"
23 m.fromNow()           // "7 years ago"
24 m.calendar()          // "03/01/2013"
25 ```
26
27 ### Add
28
29 ```js
30 m.add(1, 'day')
31 m.subtract(2, 'days')
32 ```
33
34 ```js
35 m.startOf('day')
36 m.endOf('day')
37 m.startOf('hour')
38 ```
39
40 ### Internationalization
41
42 ```js
43 .format('L')      // 06/09/2014
44 .format('l')      // 6/9/2014
45 .format('LL')     // June 9 2014
46 .format('ll')     // Jun 9 2014
47 .format('LLL')    // June 9 2014 9:32 PM
48 .format('lll')    // Jun 9 2014 9:32 PM
49 .format('LLLL')   // Monday, June 9 2014 9:32 PM
50 .format('llll')   // Mon, Jun 9 2014 9:32 PM
51 ```
52
53 See [datetime](./datetime) for more.
54
55 {% include common/moment_format.md title="Formatting" %}
56
57 ## References
58
59 ### Alternatives
60
61 * [You don't need Moment.js](https://github.com/you-dont-need/You-Dont-Need-Momentjs) _(github.com)_
62
63 ### Also see
64
65 * [Datetime cheatsheet](./datetime) _(devhints.io)_
66 * [Moment website](http://momentjs.com/) _(momentjs.com)_
67 * [Moment docs](http://momentjs.com/docs/) _(momentjs.com)_