0

I have a timestamp string and a timezone, and I need to convert that into a Date object in UTC without using moment-timezone.

Essentially, I wanna be able to do this without external dependencies:

var date = moment.tz("2021-03-03 14:40:40", "Asia/Dhaka")

Is this possible? I'd rather not have to download a rather hefty package just for this.

mylox
  • 71
  • 1
  • 5
  • Difficult without a library, but of course the libraries do it with JS so you can too if you're willing. See [*Calculate Timezone offset only for one particular timezone*](https://stackoverflow.com/questions/61361914/calculate-timezone-offset-only-for-one-particular-timezone). – RobG Mar 24 '21 at 01:55

1 Answers1

0

Check it out

let dateString = new Date("2021-03-03 14:40:40").toDateString('en-US', {timeZone: 'Asia/Dhaka'})
console.log(dateString);
Talha Noyon
  • 824
  • 7
  • 13
  • "2021-03-03 14:40:40" is not a format supported by ECMA-262 so parsing is implementation dependent. It produces an invalid date in Safari at least. This answer is also the opposite of what the OP is asking, which is to parse a timestamp in the context of a particular location. This will parse the timestamp based on the host settings, then produce an equivalent timestamp for the specified location. – RobG Nov 13 '22 at 11:10