API Exercise Tracker

Hi Everyone! :sweat_smile:

I’m finishing up my API Micro Service Exercise Tracker project, and I’ve come up with this issue with the date.

It seems that when I print to console the date shows as follows:
"date": "Fri Dec 20 2019"

This is how the example project shows the dates and that is what I’m going for:
"date": "Tue Dec 17 2019"

So I’m off to a good start…but when I look at the final result, the actual exercise that was saved to the DB it is saved in a different format:
date": "2019-12-19T23:22:25.040Z",

This is the code which saves the new exercise to the DB:

      Task.create({
        username: found.username, 
        description: req.body.description, 
        duration: req.body.duration,
        date: req.body.date ? req.body.date : Date(Date.now()).substring(0,16)
        },

If the user does not submit a date with the form, the API should use the default date at the moment.

I’ll leave my project link here: https://periodic-jonquil.glitch.me
and Github repo: https://github.com/ezzep66/Exercise-Tracker
I would appreciate any help.

Thanks!

First of all you want consistency in your DB, so you really really want to save date in one format, for example UTC:

date: new Date(req.body.date || Date.now()).toUTCString();

Displaying date in user-friendly format is not a matter of back-end, the matter of back-end is to keep the app alive :slight_smile:

Thanks!

How about when I declare the mongoose Schema?


let taskSchema = new mongoose.Schema({
  username: String,
  description: String,
  duration: Number,
  date: String
})

this used to be:
date: { type: Date, default: Date.now() }

I’m 90% sure that Mongoose will read UTC String as Date type, so you can keep that the same way