0

I have date time in this format 2023-06-23 23:00:00

need to convert it in this format Thu Jun 29 2023 22:58:50 GMT+0530 (India Standard Time)

i tried toString() it is not working.

3
  • Is this the solution to your problem? stackoverflow.com/questions/3552461/… Commented Jun 23, 2023 at 17:37
  • actually, he could use also date-fns-tz with formatInTimeZone( date, "Asia/Kolkata", "EEE MMM dd yyyy HH:mm:ss 'GMT'XXX (zzzz)" )
    – 70ny
    Commented Jun 23, 2023 at 17:47
  • Maybe just try new Date("2023-06-23 23:00:00") Commented Jun 23, 2023 at 18:01

1 Answer 1

0

The following code:

  • Parses the date string as a UTC date (repairs it as an ISO 8601)
  • Shifts the date by the time zone offset (in minutes)
  • Formats the date into the desired time zone

This does not use any external libraries, just the Intl library built into the browser.

const
  shortTimeZoneFormat = (timeZone, locale = 'default') =>
    new Intl.DateTimeFormat(locale, { timeZone, timeZoneName: 'short' }),
  longTimeZoneFormat = (timeZone, locale = 'default') =>
    new Intl.DateTimeFormat(locale, { timeZone, timeZoneName: 'long' });
    
const
  preferredLocale = 'en-US',
  preferredTimeZone = 'Asia/Kolkata',
  shortTimeZoneFormatter = shortTimeZoneFormat(preferredTimeZone, preferredLocale),
  longTimeZoneFormatter = longTimeZoneFormat(preferredTimeZone, preferredLocale);

const main = () => {
  const
    input = '2023-06-23 23:00:00', // India Standard Time (local)
    formatted = formatDate(input, preferredTimeZone);
  console.log(formatted); // Fri Jun 23 2023 23:00:00 GMT+0530 (India Standard Time)
};

const formatDate = (dateStr, timeZone) => {
  const
    asUtc = new Date(dateStr.replace(' ', 'T') + 'Z'), // Force UTC
    asLocal = shiftDate(asUtc, timeZone), // Rollback to local timeZone
    dateTime = asLocal.toLocaleString('en-US', { ...dateTimeOptions, timeZone })
      .replace(/,/g, ''),
    shortTimeZone = padOffset(stripDate(shortTimeZoneFormatter.format(asLocal))),
    longTimeZone = stripDate(longTimeZoneFormatter.format(asLocal));
  
  return `${dateTime} ${shortTimeZone} (${longTimeZone})`;
};

const shiftDate = (date, timeZone) =>
  new Date(date.getTime() - (getOffset(timeZone, date) * 6e4));

// https://stackoverflow.com/a/68593283/1762224
const getOffset = (timeZone = 'UTC', date = new Date()) => {
  const
    utcDate = new Date(date.toLocaleString('en-US', { timeZone: 'UTC' })),
    tzDate = new Date(date.toLocaleString('en-US', { timeZone }));
  return (tzDate.getTime() - utcDate.getTime()) / 6e4;
};

const stripDate = (timeZoneStr) => timeZoneStr.replace(/^\d+\/\d+\/\d+,\s*/, '');

const padOffset = (offset) => offset.replace(/(?<=[+=])(\d)(?=:)/, '0$1').replace(/:/, '');

const dateTimeOptions = {
  weekday: 'short',
  month: 'short',
  day: 'numeric',
  year: 'numeric',
  hour: '2-digit',
  minute: '2-digit',
  second: '2-digit',
  hour12: false,
};

main();

Not the answer you're looking for? Browse other questions tagged or ask your own question.