Asked  10 Months ago    Answers:  5   Viewed   330 times

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

 Answers

4

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.

Monday, August 16, 2021
 
2

You could use setlocale function before calling date function:

$newLocale = setlocale(LC_TIME, 'de_DE', 'de_DE.UTF-8');

EDIT: Quote from strftime docs:

This example will work if you have the respective locales installed in your system.

Thursday, April 1, 2021
 
footy
 
5

As far as I'm aware, there's no guaranteed way of working backwards. The best way might be to try to match regular expressions against expected well-known formats (e.g. d{2,4}[-/]d{2}[-/]d{2} for "Y-m-d") but I can't think of an easy way to do the matching without using regular expressions. You would also have to check that the parsed format makes sense, and you can't do much about ambiguous dates like 2nd of March 2009, which could be represented as 09/03/02, 2009-03-02, 02/03/09, 03/02/09, or even 09/02/03.

Thursday, April 1, 2021
 
McAn
 
3

https://github.com/jedireza/drywall

This seems promising as a good start to an admin panel.

Saturday, July 31, 2021
 
aslum
 
3

I ended up doing it like this:

Install shortId module (https://www.npmjs.com/package/shortid) Now you need to somehow stick this shortId to your objects when they're being saved in the database. I found the easiest way to do this is to append this functionality on the end of mongoose's function called "save()" (or "saveAsync()" if you promisified your model). You can do it like this:

var saveRef = Company.save;
Company.save = function() {
  var args = Array.prototype.slice.call(arguments, 0);
  // Add shortId to this company
  args[0].shortId = shortId.generate();
  return saveRef.apply(this, args);
};

So you just basically at each Model.save() function append this functionality to add shortId. That's that.

Edit: Also, I discovered that you can do it better and cleaner like this straight in Schema.

var shortId = require('shortid');
var CompanySchema = mongoose.Schema({
  /* _id will be added automatically by mongoose */
  shortId: {type: String, unique: true, default: shortId.generate}, /* WE SHOULD ADD THIS */
  name: {type: String},
  address: {type: String},
  directorName: {type: String}
});

EDIT : Now you can use the nanoid library which is much more performant and optimized. The documentation is also nice : https://github.com/ai/nanoid/

Wednesday, November 10, 2021
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :