Learn how to work with date and time objects in different time zones using JavaScript's Date constructor.
JavaScript provides the Date constructor that allows developers to create and manipulate date objects. Date objects in JavaScript are defined in UTC time. When displaying or manipulating the date object, JavaScript converts it into the user's local time zone.
const currentDateTime = new Date();
const utcDateTime = currentDateTime.toUTCString();
const pstDateTime = new Date(utcDateTime);
const offset = -8 * 60; // -8 hours * 60 minutes
pstDateTime.setMinutes(pstDateTime.getMinutes() + offset);
const options = { timeZone: 'America/Los_Angeles' }; // Timezone to PST
const formattedDateTime = pstDateTime.toLocaleString('en-US', options);
console.log(formattedDateTime);
In conclusion, this step-by-step guide will help you convert UTC date to PST date in JavaScript. By following this guide, you can display or manipulate date and time objects in any time zone based on your needs. Converting UTC to PST date is just one example of how to work with date and time objects in JavaScript, and you can modify this example to support other time zones as well.