cheatsheets/moment.md

67 lines
1.3 KiB
Markdown
Raw Permalink Normal View History

2014-06-14 04:26:51 +02:00
---
title: Moment.js
2015-11-24 06:02:17 +01:00
category: JavaScript libraries
2020-07-05 13:11:36 +02:00
updated: 2018-09-15
tags: [Featurable]
2014-06-14 04:26:51 +02:00
---
2017-10-10 14:38:30 +02:00
### Parsing
2014-06-14 04:26:51 +02:00
2017-10-10 14:38:30 +02:00
```js
2018-09-15 09:06:15 +02:00
m = moment('2013-03-01', 'YYYY-MM-DD')
2017-10-10 14:38:30 +02:00
```
2014-06-14 04:26:51 +02:00
2017-10-10 14:38:30 +02:00
This parses the given date using the given format. Returns a moment object.
2018-09-15 09:06:15 +02:00
2017-10-10 14:38:30 +02:00
### Formatting
2014-06-14 04:26:51 +02:00
2017-10-10 14:38:30 +02:00
```js
m.format() // "2013-03-01T00:00:00+01:00"
m.format('dddd') // "Friday"
m.format('MMM Do YY') // "Mar 1st 13"
m.fromNow() // "7 years ago"
m.calendar() // "03/01/2013"
2017-10-10 14:38:30 +02:00
```
2014-06-14 04:26:51 +02:00
### Add
2017-10-10 14:38:30 +02:00
```js
m.add(1, 'day')
m.subtract(2, 'days')
```
2014-06-14 04:26:51 +02:00
2017-10-10 14:38:30 +02:00
```js
m.startOf('day')
m.endOf('day')
m.startOf('hour')
```
2014-06-14 04:26:51 +02:00
### Internationalization
2017-10-10 14:38:30 +02:00
```js
.format('L') // 06/09/2014
.format('l') // 6/9/2014
.format('LL') // June 9 2014
.format('ll') // Jun 9 2014
.format('LLL') // June 9 2014 9:32 PM
.format('lll') // Jun 9 2014 9:32 PM
.format('LLLL') // Monday, June 9 2014 9:32 PM
.format('llll') // Mon, Jun 9 2014 9:32 PM
```
2014-06-14 04:26:51 +02:00
2017-10-16 20:50:18 +02:00
See [datetime](./datetime) for more.
2016-01-17 17:05:50 +01:00
{% include common/moment_format.md title="Formatting" %}
2017-10-10 14:38:30 +02:00
## References
2014-06-14 04:26:51 +02:00
2018-09-15 09:06:15 +02:00
### Alternatives
* [You don't need Moment.js](https://github.com/you-dont-need/You-Dont-Need-Momentjs) _(github.com)_
### Also see
* [Datetime cheatsheet](./datetime) _(devhints.io)_
* [Moment website](http://momentjs.com/) _(momentjs.com)_
* [Moment docs](http://momentjs.com/docs/) _(momentjs.com)_