Mongoose Date Format

Currently, the question arises of obtaining dates from mongoose. This is my diagram:

var ActivitySchema = new Schema({ activityName : String , acitivtyParticipant : String , activityType : String , activityDate : { type: Date, default: Date.now } , activityPoint : Number }); 

By default, "mm.dd.yyyy" is used, so all the data that I have in the format "dd.mm.yyyy" defaults to Date.now.

Does anyone know if there is a function "format:" dd.mm.yyyy "that I can put directly in the Scheme? Any other ideas? (Really would not like to update all the data)

Thanks for any answers.

+6
source share
1 answer

As far as I know, Mongoose does not have a "default format". Instead, it saves the Date instances as (I think) RFC 822 timestamps ( Mon Jan 02 2012 00:00:00 GMT+0100 (CET) ) and parses them from the database by running new Date(INPUT) .

The last action is your problem:

 > new Date('01.02.2012') Mon Jan 02 2012 00:00:00 GMT+0100 (CET) 

As you can see, Javascript itself parses it as mm.dd.yyyy . I don't know if this is resolvable without having to update your database.

+8
source

Source: https://habr.com/ru/post/945478/


All Articles