How to use moment.js library in angular 2 typescript app?

Sergey Aldoukhov

I tried to use it with typescript bindings:

npm install moment --save
typings install moment --ambient -- save

test.ts:

import {moment} from 'moment/moment';

And without:

npm install moment --save

test.ts:

var moment = require('moment/moment');

But when I call moment.format(), I get an error. Should be simple, can anybody provide a command line/import combination that would work?

Hinrich

Update April 2017:

As of version 2.13.0, Moment includes a typescript definition file. https://momentjs.com/docs/#/use-it/typescript/

Just install it with npm, in your console type

npm install --save moment

And then in your Angular app, import is as easy as this:

import * as moment from 'moment';

That's it, you get full Typescript support!

Bonus edit: To type a variable or property as Moment in Typescript you can do this e.g.:

let myMoment: moment.Moment = moment("someDate");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How use js library with jquery on angular2 (typescript)?

From Dev

how to use underscore.js library in angular 2

From Dev

Using moment.js in Angular 2 typescript application

From Dev

How to Include moment-timezone in Angular 2 App

From Dev

Use JS library xml2js with Angular 2

From Dev

Use JS library xml2js with Angular 2

From Dev

How to use handsontable library in angular 2

From Dev

How to use Bootstrap css Library in Angular 2?

From Dev

how to use google signin button in angular.js 2.0 typescript app?

From Dev

How to import moment.js angular2

From Dev

How to use Angular2 and Typescript in Jsfiddle

From Dev

How to use thirdparty js library in Angular 2+ without installing types?

From Dev

How to correctly bundle an Angular2-JS/Typescript app via Gulp?

From Java

How can moment.js be imported with typescript?

From Dev

Use a pure JS library in Angular

From Dev

how to use pipe in angular 2 js?

From Dev

How to use browserSync with gulp in angular2 app with angular cli?

From Dev

Is it possible to use typescript along with NW.JS and Angular.JS for desktop app

From Dev

Angular 2 - import of external leaflet typescript library

From Dev

How to use the moment library in React Bootstrap table's column definition?

From Java

How to use a typescript enum value in an Angular2 ngSwitch statement

From Dev

Typescript - how to correctly use jsPDF in Angular2?

From Dev

Typescript - how to correctly use jsPDF in Angular2?

From Dev

How to properly install Typescript, if to use in visual studio 2015 & angular 2?

From Dev

How to use Moment.JS to check whether the current time is between 2 times

From Dev

How to use a JS variable which is passed to ejs template for angular app?

From Dev

How to use a JS variable which is passed to ejs template for angular app?

From Dev

How to use p5.js sound library preload with Node.js (and create-react-app)

From Dev

Format date with moment js library

Related Related

  1. 1

    How use js library with jquery on angular2 (typescript)?

  2. 2

    how to use underscore.js library in angular 2

  3. 3

    Using moment.js in Angular 2 typescript application

  4. 4

    How to Include moment-timezone in Angular 2 App

  5. 5

    Use JS library xml2js with Angular 2

  6. 6

    Use JS library xml2js with Angular 2

  7. 7

    How to use handsontable library in angular 2

  8. 8

    How to use Bootstrap css Library in Angular 2?

  9. 9

    how to use google signin button in angular.js 2.0 typescript app?

  10. 10

    How to import moment.js angular2

  11. 11

    How to use Angular2 and Typescript in Jsfiddle

  12. 12

    How to use thirdparty js library in Angular 2+ without installing types?

  13. 13

    How to correctly bundle an Angular2-JS/Typescript app via Gulp?

  14. 14

    How can moment.js be imported with typescript?

  15. 15

    Use a pure JS library in Angular

  16. 16

    how to use pipe in angular 2 js?

  17. 17

    How to use browserSync with gulp in angular2 app with angular cli?

  18. 18

    Is it possible to use typescript along with NW.JS and Angular.JS for desktop app

  19. 19

    Angular 2 - import of external leaflet typescript library

  20. 20

    How to use the moment library in React Bootstrap table's column definition?

  21. 21

    How to use a typescript enum value in an Angular2 ngSwitch statement

  22. 22

    Typescript - how to correctly use jsPDF in Angular2?

  23. 23

    Typescript - how to correctly use jsPDF in Angular2?

  24. 24

    How to properly install Typescript, if to use in visual studio 2015 & angular 2?

  25. 25

    How to use Moment.JS to check whether the current time is between 2 times

  26. 26

    How to use a JS variable which is passed to ejs template for angular app?

  27. 27

    How to use a JS variable which is passed to ejs template for angular app?

  28. 28

    How to use p5.js sound library preload with Node.js (and create-react-app)

  29. 29

    Format date with moment js library

HotTag

Archive