Last active
February 20, 2026 21:12
-
-
Save alexjustesen/1400116943fbf1bee5d03a9b587872a5 to your computer and use it in GitHub Desktop.
Use the browser's timezone to localize timestamps for visitors using AlpineJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Livewire, Alpine } from '../../vendor/livewire/livewire/dist/livewire.esm'; | |
| Alpine.magic('dateTime', () => { | |
| return (utcString, options = {}) => { | |
| const defaults = { | |
| timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone, | |
| dateStyle: 'medium', | |
| timeStyle: 'short', | |
| ...options | |
| }; | |
| return new Intl.DateTimeFormat(window.appLocale ?? 'en-US', defaults).format(new Date(utcString)); | |
| }; | |
| }); | |
| Livewire.start(); | |
| // use in blade: <span x-text="$dateTime('{{ $model->created_at->toISOString() }}')"></span> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment