UTC转北京时间示例
<script> fetch('https://api.sunrise-sunset.org/json?lat=23.3552&lng=116.675522') .then(response => response.json()) .then(data => { const sunriseUtc = data.results.sunrise; const sunsetUtc = data.results.sunset; const dayLength = data.results.day_length; function convertUtcToBeijingTime(utcTime) { const timeComponents = utcTime.split(":"); let hours = parseInt(timeComponents[0]); const minutes = timeComponents[1]; const meridiem = timeComponents[2].split(" ")[1]; if (meridiem === "PM") { hours = (hours + 12) % 24; } hours = (hours + 8) % 24; return hours.toString().padStart(2, '0') + ":" + minutes; } const sunriseTime = convertUtcToBeijingTime(sunriseUtc); const sunsetTime = convertUtcToBeijingTime(sunsetUtc); const stsunDiv = document.querySelector('.stsun'); stsunDiv.innerHTML = `日出时间: ${sunriseTime} 日落时间: ${sunsetTime} 白昼时长: ${dayLength}`; }) .catch(error => console.error('Error fetching data:', error)); </script>
标签: UTC
评论:
2024-12-11 12:49
https://api.sunrise-sunset.org/json?lat=23.3552&lng=116.675522&tzid=Asia/Shanghai