admin管理员组文章数量:1357398
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?
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?
Share Improve this question asked Mar 10, 2020 at 23:23 ivan milenkovicivan milenkovic 991 silver badge9 bronze badges2 Answers
Reset to default 7I 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
I would strongly remend you to use Moment: https://momentjs./timezone/ is a solid library and user tested.
Hope that helps.
本文标签: javascriptOpenweather APItime always in my local time zoneStack Overflow
版权声明:本文标题:javascript - Openweather API, time always in my local time zone - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744028252a2578433.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论