Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

215
Visualizações
Why does Date.now - a given date work? Documentation shows that Date.now() returns the ms since 1970

const daysFromNow = (date) => Math.round((date-Date.now())/(1000*3600*24))

console.log(daysFromNow(new Date('07/04/2020'))); 

//result is -534 

Given the above, why does it work? I've read that .now() gives back the ms since 1970. Does the passed in date hold a value of the ms since 1970 as well?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Does the passed in date hold a value of the ms since 1970 as well?

The Date object represents a specific moment on earth, so yes you can retrieve that the milliseconds since 1970 in milliseconds.

A Date object has a valueOf function that returns the primitive value of the Date:

The number of milliseconds between 1 January 1970 00:00:00 UTC and the given date.

So (new Date()).valueOf() and Date.now() are equivalent:

console.log((new Date()).valueOf())
console.log(Date.now())

And the valueOf is called for conversion in the case of date - Date.now() because date is a Date object and Date.now() returns a number.

Here an example of a custom object with a valueOf function:

let test = {
  valueOf() {
    return 5;
  }
}

console.log(test - 1);

about 4 years ago · Juan Pablo Isaza Relatório

0

That's work because of valueOf property. That property return a number, implemented in all objects in js. You can also owerride it in your objects.

Here an example of using for creating add chain js function.

function add(x) {
  const fn = (y) => add(x + y);
  fn.valueOf = () => x;
  return fn;
}

const addTen = add(5)(5);

console.log(addTen + 1) // 11

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda