5 like 0 dislike
143 views
in Web Develop by
I want to the code of sunrise and sunset time of Dhaka, Bangladesh for html  web page

1 Answer

10 like 0 dislike
by (571 points)

Here is the code you want 

For index.html, The code is 

<div>

<div class="suntime" id="suntime"></div>

<script type="text/javascript" src="date/sunset.js"></script>

</div>

N.B: Please insert this code where you want to show the sunrise and sunset time in home page.

Now create a folder named date and in it create a file named sunset.js.

For the file sunset.js, code is bellow

 const lat = 23.8103; // Latitude for Dhaka

        const lng = 90.4125; // Longitude for Dhaka

        const apiUrl = `https://api.sunrise-sunset.org/json?lat=${lat}&lng=${lng}&formatted=0`;

        function convertToBengaliDigits(num) {

            return num.toString().replace(/[0-9]/g, (digit) => '০১২৩৪৫৬৭৮৯'[digit]);

        }

        function formatTime(date) {

            const options = { hour: 'numeric', minute: 'numeric', hour12: true };

            return date.toLocaleTimeString([], options);

        }

        fetch(apiUrl)

            .then(response => response.json())

            .then(data => {

                if (data.status === "OK") {

                    const sunrise = new Date(data.results.sunrise);

                    const sunset = new Date(data.results.sunset);

                    const sunriseTime = formatTime(sunrise);

                    const sunsetTime = formatTime(sunset);

                    document.getElementById('suntime').innerHTML = `

                        সূর্যোদয়: ${convertToBengaliDigits(sunriseTime)} | সূর্যাস্ত: ${convertToBengaliDigits(sunsetTime)}

                    `;

                } else {

                    document.getElementById('suntime').innerText = "অবস্থান পাওয়া যায়নি।";

                }

            })

            .catch(error => {

                document.getElementById('suntime').innerText = "নেট কানেকশন চেক করুন।";

                console.error(error);

            });

327 questions

98 answers

7 comments

5,034 users

1 Online Users
0 Member 1 Guest
Today Visits : 697
Yesterday Visits : 951
Total Visits : 7940687
...