Skip to content

Instantly share code, notes, and snippets.

@alexjustesen
Last active February 20, 2026 21:12
Show Gist options
  • Select an option

  • Save alexjustesen/1400116943fbf1bee5d03a9b587872a5 to your computer and use it in GitHub Desktop.

Select an option

Save alexjustesen/1400116943fbf1bee5d03a9b587872a5 to your computer and use it in GitHub Desktop.
Use the browser's timezone to localize timestamps for visitors using AlpineJS
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