0

I want to display just the date as Jun 29, 1967 - but it is coming back to my angular app as 1967-06-28 19:00:00.000 - so it is naturally displaying June 28, 1967.

My HTML form date selector - selected : 06/29/1967

The form is submitted...

MongoDB shows : 1967-06-28 19:00:00.000

I'm in central TZ, so this makes sense as a storage in UTC.

Mongo data type is "Date" defined in the mongoose schema like this:

,dob: {
    type: Date
    ,required: false
    ,default: null
}

To do the conversion at the browser, I installed - angular2-moment - as it 'advertised' a function to do this very thing. I do not get an error - but I do not get any change in display either.

DOB: {{user.dob | date:'mediumDate' }} 
DOB: {{user.dob| amLocal | amDateFormat: 'MMM DD, YYYY'}}

BOTH display

Jun 28, 1967

Ironically - the Date field of the edit form, Displays 06/29/1967...

What am I doing wrong?

j-p
  • 3,698
  • 9
  • 50
  • 93

1 Answers1

1

If you add a 'Z' to the end of the time, that makes sure it gets parsed as UTC, like this:

1967-06-28 19:00:00.000Z

The problem is that if you treat the input from your DB as UTC, you end up nine hours behind 06-29, instead of turning the date/time forward. So it would seem that your DB timestamps are actually behind UTC by several hours.

I'm in Eastern Standard Time, and it takes this:

new Date(Date.parse('1967-06-28 19:00:00.000 GMT-0900'))

...to get this:

Thu Jun 29 1967 00:00:00 GMT-0400 (EDT)

By the way, if you want to be able to convert from anything except a fixed UTC offset to your client system's local time, you'll need: https://momentjs.com/timezone/

The localizations provided by the basic moment.js don't cover time zone conversions.

kshetline
  • 12,547
  • 4
  • 37
  • 73