Change Timezone javascript

Asked

Viewed 1,178 times

0

I have an application hosted on Heroku. So, every date that is generated there in the api and returned to the front, always returns with a day delay. Already my front, is staying at Hostinger, so ends up conflicting the dates because of Timezone.

I tried that:

document.getElementById("data").addEventListener("input", function(){
    var data = document.getElementById("data").value;

  var d = new Date(data);

  var options = {week: "long", year: "numeric", month: "long", day: "numeric", timezone: "America/Sao_Paulo"}

  var year = d.getFullYear();
  var month = (d.getMonth() < 10) ? "0" + d.getMonth() : d.getMonth();
  var day = ((d.getDate() + 1) < 10) ? "0" + (d.getDate() + 1) : (d.getDate() + 1);
  var hour = d.getHours();
  var minutes = d.getMinutes();

    //options.timezone = 'America/Santa_Isabel';

  var dataFormatada = new Date(Date.UTC(year, month, day, hour, minutes, 0));
  var dataExtensa = dataFormatada.toLocaleString('pt-BR', options);
  var dataSimples = dataFormatada.toLocaleString({timezone: 'America/Sao_Paulo'});
  document.getElementById("dataSimples").innerHTML = "Data simples " + dataSimples;
})

<input type="date" placeholder="Data" id="data"/>
<p id="dataSimples"></p>

But it doesn’t work. I need any date that is created, always be the Timezone of brazil, so all the date passed to the api or created there, have this Timezone as default. Any suggestions to resolve this?

  • 1

    Do you know if the server is UTC Time? There is a js library that allows manipulation of data objects: http://momentjs.com. More specifically this function: http://momentjs.com/timezone/docs/ but to use, you must know if it is saved in UTC because its base is UTC.

  • I really can’t tell you, Athalia. That’s exactly why I’m opting for pure js in order to really persist this matter of Timezone. The server is Heroku.

  • 1

    This link: https://stackoverflow.com/questions/33995194/what-timezone-is-heroku-server-using (in English) informs that its default is UTC. It also says that there is the possibility of changing this configuration. Sorry, I’m not a big Heroku reader and to make Timezone conversion, we always have to know which Timezone is coming back.

  • Nathalia, you are an angel. Apply the setting that exists in this answer that you mentioned and it worked perfectly. Thank you very much. People like you change the world positively!!!!!!

  • 1

    Good Felipe, I can then put this item in the answer and we close this question?

  • Sure. Yes and mark as the correct answer. Thank you!

Show 1 more comment

1 answer

1


By default Heroku uses UTC Timezone.

You can swap the default server Timezone using the configuration:

heroku config:add TZ="America/Chicago"

Configuration can also be done by Dashboard.

SOURCE Stackoverflow (in English)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.