samynathanv-npm-package
Version:
A simple npm package
41 lines (31 loc) • 1.87 kB
JavaScript
// index.js
// Function to convert IST (Indian Standard Time) to Eastern Time (ET)
function convertISTtoET(istTime) {
// IST is UTC +5:30, ET is either UTC -5 (EST) or UTC -4 (EDT)
// Split the input IST time into date and time components
const [date, time] = istTime.split(' ');
const [year, month, day] = date.split('-');
const [hour, minute, second] = time.split(':');
// Create a Date object for IST (UTC +5:30)
const istDate = new Date(Date.UTC(year, month - 1, day, hour - 5, minute - 30, second));
// Convert the IST Date object to UTC and then to Eastern Time (ET)
const utcDate = new Date(istDate.toUTCString());
// Get the current date to check for daylight saving time
const currentDate = new Date();
// Determine if the current date is in Daylight Saving Time (DST) in the Eastern Time Zone
const isDST = currentDate.toLocaleString('en-US', { timeZone: 'America/New_York' }).includes("PM");
// Adjust UTC to Eastern Standard Time (EST or UTC -5) or Eastern Daylight Time (EDT or UTC -4)
let etDate;
if (isDST) {
// Eastern Daylight Time (EDT)
etDate = new Date(utcDate.setHours(utcDate.getHours() - 4));
} else {
// Eastern Standard Time (EST)
etDate = new Date(utcDate.setHours(utcDate.getHours() - 5));
}
// Return the formatted ET time in 'YYYY-MM-DD HH:mm:ss' format
const formattedET = `${etDate.getUTCFullYear()}-${String(etDate.getUTCMonth() + 1).padStart(2, '0')}-${String(etDate.getUTCDate()).padStart(2, '0')} ${String(etDate.getUTCHours()).padStart(2, '0')}:${String(etDate.getUTCMinutes()).padStart(2, '0')}:${String(etDate.getUTCSeconds()).padStart(2, '0')}`;
return formattedET;
}
// Export the function for use in other files
module.exports = { convertISTtoET };