Posts

Showing posts from July, 2024

Us Time Handle

let newDate ; // 1. Determine User's Time Zone (if possible) if ( langCode ) {   try {     newDate = moment . utc ( date ). tz ( langCode );   } catch ( error ) {     console . error ( "Error converting date to user's time zone:" , error );     // Handle the error (e.g., use UTC time or display an error message)   } } else {   // 2. Handle Unknown Time Zone (improved heuristic)   const offset = moment . utc ( date ). utcOffset () / 60 ; // Get UTC offset in hours   // Consider a mapping of offsets to time zones (replace with your data)   const timeZoneMapping = {     '-4' : 'US/Eastern' , // Eastern Time (DST)     '-7' : 'US/Pacific' ,   // Pacific Time (DST)     // Add other time zones as needed   };   const potentialTimeZone = timeZoneMapping [ offset . toString ()];   if ( potentialTimeZone ) {     newDate = moment . utc ( date ). tz ( potentialTimeZone );   } else {     // Handle cases where no matching offset