Saturday, 21 September 2019

Darksky api not displaying correct time

I used the Dark Sky API variable 'time' to retrieve the unix timestamp, then converted it to hour with the following code:

        if(navigator.geolocation){
            navigator.geolocation.getCurrentPosition(position => {
                long = position.coords.longitude;
                lat = position.coords.latitude;

                const proxy = "https://cors-anywhere.herokuapp.com/";
    const api = `${proxy}https://api.darksky.net/forecast/7fa728a1a158d84bf2c85bbeb53ddaea/36.778259,-119.417931`;

                fetch(api)
                    .then(response => {
                        return response.json();
                    })
                    .then(data => {
                        /*console.log(data);*/
                        const {temperature, summary, icon, time} = data.currently;

                                //Get date from time variable
                                var date = new Date(time*1000);
                                // Get hours from date
                                var hours = date.getHours();
                                // Get minutes from date
                                var minutes = "0" + date.getMinutes();
                                // Get seconds from date
                                var seconds = "0" + date.getSeconds();
                                // Display time in 10:30:23 format
                                var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);

                                console.log(formattedTime);

                                //Check time
                                if (hours <= 19 && hours >= 7) {
                                //Day
                                } else {
                                //Night
                                }
});

Everything else is working perfectly but it's still not displaying the correct time of the retrieved location data in the console.



from Darksky api not displaying correct time

No comments:

Post a Comment