0

I am including data from Openweather API which returns this, among other things:

dt: 1583876444
sys:
   country: "ES"
   sunrise: 1583822046
   sunset: 1583864165
timezone: 3600

My solution for timezone was to divide this number with 3600:

`Timezone: GMT ${data.timezone / 3600}:00 `

This somehow works and I get for example 'Timezone: GMT 9:00' although for some places like Tehran I get 'Timezone: GMT 3.5:00' and it is not ideal obviously. Any better solution for this?

For sunrise/sunset, I did this:

let date = new Date(data.sys.sunrise * 1000).toString();
  let sunrise = date.slice(16, 24);
...
`<h1>${sunrise}</h1>`

And I get what I want, like 03:50:36

Problem is it is always my local time, it shows me when sunrises in Tokyo but in my local time and not in Tokyo's local time. Obviously I would like to get Tokyo's local time. Is this doable?

2 Answers 2

6

I faced the exact same problem with openweather. Add the timezone to the time and then times by 1000.

for sunrise/sunset:

const sunrise = new Date((data.sys.sunrise + data.timezone) * 1000)

You will get the local time

0

I would strongly recommend you to use Moment: https://momentjs.com/timezone/ is a solid library and user tested.

Hope that helps.

1
  • I tried but it is not what I need, at least I can't find it :( Mar 12, 2020 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.