0
votes

var res= "03/19/2020 13:15:00"; // EST time

when i am converting to local timezone

moment(moment(res.date, 'MM/DD/YYYY HH:mm:ss').utc()).local().format("MM DD YYYY HH:mm:ss")

I tried many things. But I don't know how to convert Est date time to local date time in javascript.

1
No. It is not helpful @HereticMonkeypriya_singh

1 Answers

2
votes

Presumably EST is US Eastern Standard Time, which is GMT+5. Most places within that timezone also observe daylight saving, so you may need to determine which set of rules to use. The offset should be passed with the rest of the timestamp to remove that as a source of error.

Timezone offsets are usually expressed as the time to add to UTC to get the local time, though ECMAScript (and other programming languages) reverse that, i.e. the time to subtract from UTC to get local.

If the offset of the timestamp is known, then you can parse the string as UTC, then apply the offset, then use plain methods to get local values based on host system settings, e.g.

// Offset in minutes, +ve east
function parseWithOffset(s, offset) {
  let b = s.split(/\D/);
  let d = new Date(Date.UTC(b[2], b[0]-1, b[1], b[3], b[4], b[5]));
  d.setUTCMinutes(d.getUTCMinutes() - offset);
  return d;
}

let s = '03/19/2020 13:15:00'; // US EST
let d = parseWithOffset(s, -300);
console.log(d.toString());