How Should I Store/Display My Date/Time Field?

I know there's a ton of questions around date/time storage and formatting, but I'm struggling to get the right approach. My app is a Node.js app with Express using mongoose with a MongoDB backend. I'm using moment.js to format my date/times.

The schema in question looks like:

const stateSchema = new mongoose.Schema({
  changed: {
    type: Date,
    default: new Date()
  }
});

Open in new window

Based on the docs, I was expecting the date to be stored in UTC by default, but instead I see this:
changed: 2018-08-15 17:26:14.499

Open in new window

I'm displaying the date/time with something like (pug):

p= `Changed: ${moment(state.changed).format('MMMM Do YYYY, h:mm:ss a')}`

Open in new window

On localhost, the app shows "August 15th 2018, 5:26:14 pm" but on the hosted Heroku app I get "August 15th 2018, 10:26:14 pm." I am expecting "August 15th 2018, 5:26:14 pm" because I'm opening the Heroku app in the same timezone, but it looks like it's giving me the server timezone.

What should I do to make sure the date/time shows up in the client's local timezone?
LVL 12
Michael VasilevskySolutions ArchitectAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PatHartmanCommented:
How a date is stored in a database is one thing.  How it is displayed is another.  The display of dates is normally controlled by the user's Windows settings.  If you store a date as UTC, you would have to convert it for display by determining the user computer's time zone and whether or not they used DST.
Michael VasilevskySolutions ArchitectAuthor Commented:
Maybe my question then boils down to how to determine the client's timezone and convert the UTC date in a Node.js app?
PatHartmanCommented:
If I knew how to do that for your platform, I would have included the code.  This question came to me because I answer SQL questions but I develop using MS Access.  So, I understand the problem but cannot provide any code.  Sorry.
Michael VasilevskySolutions ArchitectAuthor Commented:
I think this is the answer. It gives me the wrong time on localhost, but should be the correct time on the hosted app, no matter where I am.

Add offset to schema:

    const stateSchema = new mongoose.Schema({
      changed: {
        type: Date,
        default: new Date()
      },
      offset: {
        type: Number,
        default: new Date().getTimezoneOffset()
      }
    });

Open in new window


Then subtract the offset when displaying:

    p= `Changed: ${h.moment(state.changed.getTime() - (state.offset * 60000)).format('MMMM Do YYYY, h:mm:ss a')}`

Open in new window

Michael VasilevskySolutions ArchitectAuthor Commented:
See above

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Node.js

From novice to tech pro — start learning today.