-1

Please don't suggest any libraries or any node.js stuff. I want to be able to find a method that is based on pure plain JavaScript OK

so I'm creating a feature on my website that coverts past time and date from the one-time zone into another time zone. For example, I have this time and date from mountain standard time (MST). 6/29/2011 4:52 PM and I want to know what time and date it was at that moment in Arabia Standard Time (AST).

I know that the date may be different because countries on earth is a day ahead or behind from other countries. I just can't figure out how this can be done in plain JavaScript.

This is my code so far

//???
#container{
background-color: red;
display: inline-block;
margin-bottom: 20px;
padding: 8px;
border-radius: 8px;
}

input{
  display: block;
  margin-bottom: 10px;
}

p{
  margin-bottom: 0;
}
<h1>Convert past date and time from one time zone into another.</h1>

<div id='container'>
  <p>Date</p>
<input type='text' id='date' value='6-29-2011'>
  <p>Time</p>
<input type='text' id='time' value='4:52 PM'>
  <p>Time zone</p>
<input type='text' id='time-zone' value='MST'>
  
<p>Convert into this time zone</p>
<input type='text' value='AST'>
  
  <p>Result</p>
  <em id='result'></em>
</div>

According to this time zone converter website https://www.timeanddate.com/worldclock/converter.html This will be the converted time and date 6/29/2011 4:52 PM MST and in AST 6/30/2011 1:52 AM

  • 2
    Possible duplicate of [Convert date to another timezone in JavaScript](https://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript) – Quentin Veron Jun 11 '19 at 20:29
  • What's the question? (not to be rude) What have you tried? _//???_ Putting a question mark on the javascript part and making us write all the code is not a good thing. Research and try something yourself. – weegee Jun 11 '19 at 20:30
  • I'm not satisfied with other methods because there mostly based on the present time and date and not on the past. Well that is up to you if you want to help but I appreciate your effort into trying to help me out. –  Jun 11 '19 at 20:47

1 Answers1

-1

Use timeZone property, like this:

var convertTime = new Date(2011,06,29,4,52).toLocaleString("en-US", {timeZone: "MST"});
convertTime = new Date(convertTime);
console.log('Converted time: '+convertTime.toLocaleString())

Source: Convert date to another timezone in JavaScript

Dorian Mazur
  • 514
  • 2
  • 12
  • I actually visited that link but that method goes based on the present not in the past. –  Jun 11 '19 at 20:48
  • How can I make it goes based in the past for example, according to this time zone converter website https://www.timeanddate.com/worldclock/converter.html This will be the converted time and date 6/29/2011 4:52 PM MST and in AST 6/30/2011 1:52 AM –  Jun 11 '19 at 20:51