Time and dates
In this document we address issues and use cases related to dealing with date and times.
#
Date and times in javascriptTo deal with date and times in JavaScript the suggested library (used also in @acanto/core
) is date-fns
, it is functional and tree-shaking optimized.
#
Convert datetimes to localOften times you want displaying to the user a date or time of a certain event not exactly as it arrives from the server but translated to the user' local time. This can be achieved through a combination of server and client data as the latter is the only one reliably knowing the local time of the user.
#
ExampleAssuming you have a php valid $date
variable in your blade
template you first need to translate it milliseconds (to prevent invalid JavaScript Date
objects e.g. in Safari), then in whatever HTML element use the attribute data-datelocal
to define the desired datetime format and inside it (as its textContent
) print the just transformed milliseconds date.
In your blade.php
template:
Then in your .js
file:
#
Date formatsAvailable date formats follow date-fns
documentation.