Any way to specify format to moment.js but keep local timezone adjustment?

Any way to specify format to moment.js but keep local timezone adjustment?

paulh736paulh736 Posts: 9Questions: 6Answers: 0

Test case below. My real-life example is working server-side but I don't think that affects it.

I've got a DataTable with date/time data coming in like e.g. 2022-09-21T08:54:22 There's no time zone information included but the times are all UTC.

If I do

render: DataTable.render.datetime(),

(as in the test case), then DataTables and moment.js do a fab job of recognising the UTC date/time and adjusting the time to the local time zone (e.g. my client is currently on BST (GMT+1) so it's adding an hour to the UTC times). That's exactly what I want.

I want to alter the output format a little so I changed it to

render: DataTable.render.datetime('D MMMM YYYY, HH:mm:ss')

and that works too, but now it has stopped doing the time zone adjustment and it's just outputting the UTC time. Is there some way I can specify the output format, but still have it adjust the time for the local time zone on the client?

http://live.datatables.net/lireluta/2/edit?html,css,js,output

Answers

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421
    edited September 2022

    I think you can't do that. If I recall it correctly you will only have the automatic adjustment if you keep
    DataTable.render.datetime()
    without parameter.
    You will need a more complex renderer that allows you to use the output of
    DataTable.render.datetime()
    as an input parameter OR you adjust the input date format returned from the server to match your target format.

    https://datatables.net/examples/datetime/auto-locale-moment.html

Sign In or Register to comment.