Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose date format

Currently having an issue retrieving dates from mongoose. This is my schema:

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

This defaults to use "mm.dd.yyyy", so all data I have which is in format "dd.mm.yyyy" defaults to Date.now.

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

Thank you for any replies

like image 820
Are Avatar asked May 21 '13 07:05

Are


1 Answers

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

That 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 that's solvable without having to update your database.

like image 145
robertklep Avatar answered Oct 13 '22 07:10

robertklep