Skip to main content
AngularJavaScriptTips

Get the difference between dates with momentJS

By December 9, 2019No Comments

First of all, let’s import momentJS to our project:

import * as moment from 'moment';

Now that we’re ready to use the moment() method we can use the following snippet:

const now = moment(new Date()); //todays date
const end = moment(this.fecha, 'DD/MM/YYYY'); // another date
const duration = moment.duration(end.diff(now));
const days = Math.round(duration.asDays());

Note that I’m using Math.round() that is because the return of moment.duration() could be with fractional digits so in order to just get days we use it.

Sebastián Becerra

Sebastián Becerra

Trabajo con tecnologías web en arquitecturas Cloud. Me gusta descubrir y aprender nuevas tecnologías en Layouts (CSS, Sass, Less), Frameworks de Desarrollo (JQuery, AngularJS, Ionic) y plataformas Web (Wordpress, Joomla, Prestashop). He colaborado con diferentes proyectos de software libre y código abierto con fines sociales. Además participé en distintos eventos TI exponiendo sobre comunidades y educando sobre tecnologías Web.

Leave a Reply