如何使用开放天气API获取搜索城市的实时本地日期和时间



我已经使用开放天气API创建了一个天气项目:https://openweathermap.org/current?fbclid=IwAR1SVc9zn9uXaZWLmJA9lYEeZvUc1s_kR68RFadWuIwd8yBjIyJ7zsVMKkE

我已经将所有API参数添加到我的代码中。

但是,我也想获取正在搜索的城市的实时本地日期和时间。

例如,当用户搜索";雅典";它显示的内容如下:

2022年1月15日星期六16:24:03(我希望秒数每过一秒就改变直播(

我的代码:

javascript和HTML脚本插入

let weather = {
apiKey: "XXXXXXXXXXXXXXXXXXXXX",
fetchWeather: function (city) {
fetch(
"https://api.openweathermap.org/data/2.5/weather?q=" +
city +
"&units=metric&lang=en&appid=" +
this.apiKey
)

.then((response) => {
if (!response.ok) {
alert("No weather found.");
throw new Error("No weather found.");
}
return response.json();
})
.then((data) => this.displayWeather(data));
},

//fetching the API parameters:
displayWeather: function (data) {
const { name } = data;
const { lon } = data.coord;
const { lat } = data.coord;
const { icon, description } = data.weather[0];
const { feels_like } = data.main;
const { temp, humidity } = data.main;
const {temp_min} = data.main;
const {temp_max} = data.main;
const { pressure } = data.main;
const { speed } = data.wind;
const { deg } = data.wind;
const { visibility } = data;
const { all } = data.clouds;
const { gust } = data.wind;
const {timezone} = data;
const { sunrise } = data.sys;
const { sunset } = data.sys;

//Displaying the results:
document.querySelector(".city").innerText = "Weather: " + name;
document.querySelector(".lon").innerText = "Longitude: " + lon;
document.querySelector(".lat").innerText = "Latitude: " + lat;
document.querySelector(".icon").src =
"https://openweathermap.org/img/wn/" + icon + ".png";
document.querySelector(".description").innerText = description;
document.querySelector(".temp").innerText = temp + "°C";
document.querySelector(".feels-like").innerText = "Temperature feels like: " + feels_like + "°C";
document.querySelector(".temp_min").innerText = "Minimum Temperature: " + temp_min + "°C";
document.querySelector(".temp_max").innerText = "Maximum Temperature: " + temp_max + "°C";
document.querySelector(".humidity").innerText =
"Humidity: " + humidity + "%";
document.querySelector(".visibility").innerText = "Visibility: " + visibility + " meters";
document.querySelector(".cloudiness").innerText = "Cloudiness: " + all + "%";
document.querySelector(".pressure").innerText = "Atmospheric Pressure: " + pressure + " hPa";
document.querySelector(".wind").innerText =
"Wind speed: " + speed + " km/h";
document.querySelector(".wind-deg").innerText = "Wind degrees: " + deg + "°";
document.querySelector(".wind-gust").innerText = "Wind gust: " + gust + " m/s";
document.querySelector(".sunrise").innerText = "Sunrise: " + window.moment(sunrise * 1000).format('HH:mm a');
document.querySelector(".sunset").innerText = "Sunset: " + window.moment(sunset * 1000).format('HH:mm a');
document.querySelector(".weather").classList.remove("loading");
document.body.style.backgroundImage =
"url('https://source.unsplash.com/1600x900/?')"; 
},

search: function () {
this.fetchWeather(document.querySelector(".search-bar").value);
},
};
document.querySelector(".search button").addEventListener("click", function () {
weather.search();
});
document
.querySelector(".search-bar")
.addEventListener("keyup", function (event) {
if (event.key == "Enter") {
weather.search();
}
});
//Displaying Athens weather on page load
weather.fetchWeather("Athens");
<script src="https://momentjs.com/downloads/moment.js"></script>
<!-- script to convert sunrise and sunset times to time format and on the local time of the searched city -->
<script src="./script.js" defer></script>
<!-- local script -->

我应该对代码做哪些修改?

注意:如果可能的话,我想要现场直播的日期和时间

您可以试用Time API
使用此API,您可以获取地球上任何位置的当前时间时区信息转换时区并列出特定位置的所有夏令时(DST(更改。

根据文档(使用cURL实现(:

$ ACCESSKEY="<Your Access Key>"
$ SECRETKEY="<Your Secret Key>"
$ curl -G 
--data-urlencode "version=3" 
--data-urlencode "prettyprint=1" 
--data-urlencode "accesskey=$ACCESSKEY" 
--data-urlencode "secretkey=$SECRETKEY" 
--data-urlencode "placeid=norway/oslo" 
https://api.xmltime.com/timeservice

获取API参数(显示天气(

const {timezone} = data;
const {dt} = data;
const dateTime = new Date(dt * 1000);
const toUtc = dateTime.getTime() + dateTime.getTimezoneOffset() * 60000;
const currentLocalTime = toUtc + 1000 * timezone;
const selectedDate = new Date(currentLocalTime);
const date = selectedDate.toLocaleString("en-GB", {
day: "numeric",
weekday: "long",
month: "long",
});
const year = selectedDate.toLocaleString("en-GB", {
year: "numeric",
});
const hour = selectedDate.toLocaleString("en-GB", {
hour: "2-digit",
minute: "2-digit",
hour12: false,
});
return `${date} ${year} ${hour}`; //Thursday, 21 July 2022 18:14

最新更新