Compare Angular Data Js

Asked

Viewed 900 times

0

I would like to compare two dates at angular js, the two dates are like this:

Sat Oct 14 2017 11:43:46 GMT-0300 (Hora oficial do Brasil)
Sat Oct 14 2017 17:53:51 GMT-0300 (Hora oficial do Brasil)

How can I buy these two dates?

1 answer

1

Date comparison can be done using pure Javascript through Date class objects.

const data1 = new Date('Sat Oct 14 2017 11:43:46 GMT-0300 (Hora oficial do Brasil)');
const data2 = new Date('Sat Oct 14 2017 17:53:51 GMT-0300 (Hora oficial do Brasil)');

const datasIguais = data1.getTime() === data2.getTime();  -- false
const datasDiferentes = data1.getTime() !== data2.getTime(); -- true
const primeiraDataAnteriorSegunda = data1.getTime() < data2.getTime(); -- true
const primeiraDataPosteriorSegunda = data1.getTime() > data2.getTime(); -- false

Note that you should always make the comparison from the return of the getTime() method of the Date class that returns the time value that is the number of milliseconds since 1 January 1970 (UTC). Do not make comparisons directly between variables, as they are only references to the instantiated objects.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.