Craft By Zen

Curation  /  The Stream  /  Start and end of day offsets for current system time zone offset using Luxon

Start and end of day offsets for current system time zone offset using Luxon

Published

Luxon datetime library defaults startOf and endOf methods to UTC. But this isn’t great for end users who don’t live in UTC / GMT. To offset this, we need to grab the time zone offset from the user’s system.

const systemTimeZoneOffsetInMinutes = new Date().getTimezoneOffset();  
const systemTimeZoneOffsetInHours = systemTimeZoneOffset / 60;

Then we can add the offset to the Luxon datetime object.

const now = DateTime.now();
const startOfLocalDay = now.startOf('day').plus({ hours: systemTimeZoneOffsetInHours });
const endOfLocalDay = now.endOf('day').plus({ hours: systemTimeZoneOffsetInHours });