Menu

Converting recurring game event times to the player's local time zone in JavaScript
📰

Converting recurring game event times to the player's local time zone in JavaScript

DEV Community·Owen Parker·1 day ago
#NoF9w2vF
#dev#const#zone#date#article
Reading 0:00
15s threshold

Live-service games love recurring events that start at a fixed UTC time. Players, though, live in every time zone, and "the event starts at 18:00 UTC" is a common source of confusion. Here is a small, dependency-free way to show event times in the visitor's own zone.

1. Store the schedule in UTC

// Daily event windows, defined once in UTC const EVENT_WINDOWS_UTC = [ { start : "14:00" , durationMin : 60 } , { start : "20:00" , durationMin : 60 } , ] ;

Enter fullscreen mode

Exit fullscreen mode

2. Build the next occurrence as a real Date

function nextOccurrence ( { start , durationMin } , now = new Date ( ) ) { const [ h , m ] = start . split ( ":" ) . map ( Number ) ; const d = new Date ( Date . UTC ( now . getUTCFullYear ( ) , now . getUTCMonth ( ) , now . getUTCDate ( ) , h , m ) ) ; if ( d . getTime ( ) + durationMin * 60000 < now . getTime ( ) ) d . setUTCDate ( d . getUTCDate ( ) + 1 ) ; return d ; }

Enter fullscreen mode

Exit fullscreen mode

3.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.