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

417
Visualizações
Passing current time to Mongoose query

I've run into problem. I made field in my Mongoose schema with type "Date":

...
timeOfPassingQuestion: Date,
...

Now, I want to pass current time in hours, minutes, seconds and miliseconds and save it into that field. How should I format my Node variable so I can pass it without errors?

Edit: Also, I forgot to say that I wanna later see how much time user spent answering question by subtracting current time and time that I pulled from DB, timeOfPassingQuestion field.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This is the syntax to create a schema that supports a date field:

// Schema
{ //...
  someDate: Date,
}

// date object that you can use whenever you decide to set it
var dateObj = new Date();

This will create a JavaScript date object that you can then pass into your Mongoose object for the date field.

Or, if you will always want it on creation, put it directly in your mongoose schema

{  //...
   createdDate: { type: Date, default: Date.now },
}

In order to compare the time in the future, I suggest you use moment.js, then you can query the time difference like so:

moment(Model.createdDate).fromNow();

Sources:

Mongoose Schema

Moment.js fromNow

over 4 years ago · Santiago Trujillo 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