Working with Date and Time in NodeJS, Mongoose, and TypeScript

-1

Good morning everyone, 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 in NodeJS.

Here is an example of the template 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 do not know what to put.

  • In the dataset field I need to record only dates, not the time.
  • In the StartTime and EndTime fields I need to store only hours, no dates.
  • In the DateTime field I need to store the moment something happened (date and time).

How do you do this?

    
asked by anonymous 23.12.2018 / 15:57

1 answer

0

A good way for you to do this is by reviewing the documentation ( link and link ), I'll try to synthesize for you.

I think it's a limitation of Mongoose (I do not use the same, but I know JS well and I studied a bit about it to answer you), what you can do is:

  • In the DateTest field, you save the complete date, but when using it, just use the date and disregard the hours (watch out for the timestamp);

  • In the StartTime and EndTime fields, do the same store the date and time and only use the hours, so I understand the beginning and end of your event will happen on the same day, so it can be done like this

A good request for you to handle dates in JS is the moment ( link ), I always use it.

I hope I have helped.

Here's an example of what your source code looks 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 tip from Dev to Dev, in my point of view, one of JS / TypeScript's great gifts is to be a language where you can do a lot by writing less, compared to JAVA for example, I believe that the module you wrote could be less.

    
27.12.2018 / 19:57