0

Here I want to take the time zone 'America / Santiago'... But i get 'invalid date' result, why?

Remember, I just want to retrieve the time data, from the 'new Date ()' function, not from the Javascript Libraries

And remember I do not want to take client time automatically, I want to take the time zone 'America / Santiago', but I'm from Indoneisa

Not the 'getTimezoneOffset' function, but using the 'new Date ()' function, I also know the 'getTimezoneOffset' function, but this function takes the timezone automatically depending on which user is in which time zone.

function showTheTime() {
  var da_te = new Date('America/Santiago'); 
  document.getElementById("result").innerHTML = da_te;
}
showTheTime();
<p id="result"></p>
JLRishe
  • 99,490
  • 19
  • 131
  • 169
Firmansyah
  • 106
  • 12
  • 27
  • 1
    `America/Santiago` is not a `Date`. – Steve May 16 '17 at 08:08
  • 1
    There is no way to set timezone to `Date`. Javascript `Date` provides a method `setTimezoneOffset ` to get system timezone though, there is no `setTimezoneOffset `. – suish May 16 '17 at 08:10
  • _“But i get 'invalid date' result, why?”_ – because you are making stuff up, and for some inexplicable reason expect that to work. `new Date('America/Santiago')` makes no sense whatsoever. – CBroe May 16 '17 at 08:11
  • @suish, I think this only generates the time automatically, which I say I want to generate time in the 'America / Santiago' time zone, but I'm from Indonesia – Firmansyah May 16 '17 at 08:12
  • Good question, I also want to ask something like this .... maybe the others know more :) – Angular San May 16 '17 at 08:27

1 Answers1

3

It isn't possible to set the timezone of the built-in Date object. Its timezone is always the local timezone.

However, the Moment Timezone plugin for moment.js will allow you to change a moment to a specific timezone.

Example:

function showTheTime() {
    var da_te = moment().tz('America/Santiago'); 
    document.getElementById("result").textContent = da_te.format('d/M/YY h:mma');
    console.log(da_te.valueOf());
}

showTheTime();
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data.min.js"></script>
<p id="result"></p>
JLRishe
  • 99,490
  • 19
  • 131
  • 169