How to work with Date & Time in Nodejs, Mongoose and Typescript

Asked

Viewed 2,627 times

-1

Good morning, you guys, I come from the Java world and am starting in Nodejs.

I’m having a hard time understanding how to work with dates and times at Nodejs.

This is an example of the model I want to use:

    export interface teste extends mongoose.Document {
        descricao: string,
        dataTeste: ????,
        horarioInicial: ????,
        horarioFinal: ????,
        dataHoraRegistro: ????
    }

    const testeSchema = new mongoose.Schema({
        descricao:{
            type: String,
            required: true,
            maxlength: 200,
            minlength: 3
        },
        dataTeste:{
            type: ?????,
            required: true
        },
        horarioInicial:{
            type: ?????,
            required: true
        },
        horarioFinal:{
            type: ?????,
            required: true
        },
        dataHoraRegistro:{
            type: ?????,
            required: true
        }
    }

export const Teste = mongoose.model<Teste>('Teste', testeSchema)

In all the places I left ??? I don’t know what to put.

  • In the dataThis field I need to record only dates, without the time.
  • In the time fieldsInitial and scheduleFinal I need to store only hours, no dates.
  • In the Datemark field I need to store the moment something happened (date and time).

How do you do it?

1 answer

1

A good way to achieve this is by analyzing the documentation(https://www.npmjs.com/package/mongoose and https://mongoosejs.com/docs/schematypes.html#Dates), I’ll try to synthesize for you.

I believe to be a limitation of Mongoose (I do not use the same, but I know JS well and studied a little about it to answer you), what you can do is the following:

  • In the Date field, you save the full date, but when using, only use the date and disregard the time (watch out for the timestamp);

  • In the timesInitial and timerFinal, do the same store the date and time and only use the hours, so I understood the beginning and end of your event will be the same day, so it can be done like this;

  • In the DateRegister field you also arrow as Date and add the default property with the value Date.now;

A good request for you to manipulate dates in JS is Moment (https://momentjs.com/), always use.

I hope I’ve helped.

Below is an example of what your source code would look like:

export interface teste extends mongoose.Document {
    descricao: string,
    dataTeste: Date,
    horarioInicial: Date,
    horarioFinal: Date,
    dataHoraRegistro: Date
}

const testeSchema = new mongoose.Schema({
    descricao:{
        type: String,
        required: true,
        maxlength: 200,
        minlength: 3
    },
    dataTeste:{
        type: Date,
        required: true
    },
    horarioInicial:{
        type: Date,
        required: true
    },
    horarioFinal:{
        type: Date,
        required: true
    },
    dataHoraRegistro:{
        type: Date,
        default: Date.now,
        required: true
    }
}

export const Teste = mongoose.model<Teste>('Teste', testeSchema)

A advice from Dev to Dev, in my view, one of the great gifts of JS/Typescript is to be a language where you can do a lot less writing, when compared to JAVA for example, I believe that the module you wrote could be smaller.

  • Man, what a fantastic response! The tip was excellent. I had put the time as a string, but then I wasn’t sure how to make calculations with the hours. I will also try to reduce the amount of code. Thank you very much for the attention on the answer.

  • I’m glad I helped! : D

Browser other questions tagged

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