0

While outputting a date field through Handlebars to HTML on the server-side, it's giving me a different format than what's visible in the API when debugging. I've tried using both double and triple braces for output. I need that pure UTC time.

API DATA

data: {
  date:
    html: "<time>Sun Apr 28 2019 19:00:00 GMT-0500 (CDT)</time>",
    json: {
      value: "2019-04-29T00:00:00.000Z"
    }
  },
  title: {
    html: "<h3>Blah blah</h3>",
    json: {...}
  }
}

TEMPLATE (Handlebars)

<div data-date="{{{ data.date.json.value }}}">
  {{ getTextField data 'title' }}
</div>

EXPECTED OUTOUT

<div data-date="2019-04-29T00:00:00.000Z">
  <h3>Blah blah</h3>
</div>

ACTUAL OUTPUT

<div data-date="Sun Apr 28 2019 19:00:00 GMT-0500 (CDT)">
  <h3>Blah blah</h3>
</div>
doublejosh
  • 5,548
  • 4
  • 39
  • 45
  • The date in the value is GMT +0000 your local is GMT -0500 so it is showing correctly. – James Khoury May 02 '19 at 10:39
  • This is server-side rendering. – doublejosh May 02 '19 at 20:50
  • it is rendering it exactly as you asked it to. I think you need to register a helper in handlebars to format the date. – James Khoury May 02 '19 at 21:00
  • It's not. It's converting the string "2019-04-29T00:00:00.000Z" into "Sun Apr 28 2019 19:00:00 GMT-0500 (CDT)" when outputting the field value to a template. How can I keep the UTC time value? – doublejosh May 02 '19 at 23:43
  • it's converting the date (not string) to a string format determine by your locale. I recommend you make a handlebars helper to display in UTC – James Khoury May 02 '19 at 23:47
  • This is API response data, a date object is never created. Are you saying when Node encounters this type of string value in an API response, it interprets that into a date object and outputs another format automatically when used... this date object outputs one way when the parent is console logged and another via templates? – doublejosh May 03 '19 at 04:43
  • 1
    Logging to the console might format it differently to handlebars. I haven't looked at the internals of either. I think you could see this by trying the `typeof` operator on it but if it is a string then handlebars is converting it to a date then formatting it as you see it. – James Khoury May 03 '19 at 04:55
  • None of this is in the console. This is the rendered HTML via Handlebars on the server-side. – doublejosh May 14 '19 at 21:41
  • So add some? or log it somewhere else maybe even to a file? – James Khoury May 14 '19 at 21:43
  • Your suggestion is to add another type of debug that will likely to change the value while outputting. This doesn't seem useful. – doublejosh May 29 '19 at 00:35
  • If the value is changing due to logging then something is seriously wrong. I suggested the extra logging to show you that the value hasn't changed. Javascript will format dates with local culture settings by default which is what you're seeing. – James Khoury May 29 '19 at 00:45

0 Answers0