Javascript/Typescript Date and time Operations: Examples using Moment.js

Javascript/Typescript Date and time Operations: Examples using Moment.js

Last updated:

WIP Alert This is a work in progress. Current information is correct but more content will probably be added in the future.

Vanilla Date class

var date = new Date()
date.toString()
// "Wed Aug 10 2016 21:44:00 GMT-0300 (BRT)"

Using moment.js

There are many ways to install and use moment.js

For Typescript, just do npm install moment and then import it like this: import * as moment from 'moment';

Moment.js is a library that provides a better implementation of the vanilla Date class with many useful

// equivalent to calling new Date()
var date = moment();
date.toString()
// "Wed Aug 10 2016 21:44:00 GMT-0300 (BRT)"

// but you get extra methods in the object as well
date.fromNow()
// "a few seconds ago"

Date Math

TODO

Timezones

TODO

Durations (date periods)

TODO

Formatting + displaying dates

TODO

References