0

I am using the open weather map API and I'm trying to figure out how to convert the date format which is by default "yyyy-mm-dd hh:mm:ss".

I would like to simply display the day such as "Tuesday" rather than the long output.

This is my first time working with an API and I'm just trying to learn. I have researched other topics on how to do this however I can't find any sources. Maybe I just don't know what exactly I should be looking for.

My code right now looks like this...

The final forecast function is where I am outputting the date with dt_txt.

    function forecastRequest() {

        //MAKE THE AJAX REQUEST

            $.ajax( {
                //DISPLAY WITH ZIPCODE API CALL
                url:'http://api.openweathermap.org/data/2.5/forecast?zip=49009' + ",us&units=imperial" +
                "&APPID=d9347e4e650b022f26f0990856107ac1",
                type: "GET",
                dataType: "jsonp",
                success: function(data){
                    //LOG THE SUCCESS FUNTION TO LET US KNOW IF WE ARE GETTING THE API CALL
                    console.log(data);

                    //SHOW THE DATA RESULTS FROM THE SHOW FUNCTION
                    var widget2 = forecast(data);

                    $("#forecast").html(widget2);


                }
            });
    } // End ajaxRequest Function

}); // End Document.Ready

//DISLAY THE RESULTS
function show(data){
    return   "<h1 class='temp'>" + data.main.temp + "&#176;</h1>" ; 

}

function forecast(data) {
    return   "<p class='temp'>" + data.list[0].main.temp + "&#176; " + data.list[0].dt_txt + "</p>" +
             "<p class='temp'>" + data.list[1].main.temp + "&#176; " + data.list[1].dt_txt + "</p>";
}
Casey Smith
  • 13
  • 2
  • 7

1 Answers1

-1

There is no specific method to get a day for a particular date. You can use momentjs library for it and can get the date format.

    moment().format('dddd')

This would give you the day.

  • Re: "*There is no specific method to get a day for a particular date*". There is the [*getDay*](http://ecma-international.org/ecma-262/8.0/#sec-date.prototype.getday) method which returns the day number in the week, and also [*toLocaleString*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString) with the *weekday* option set to ether long or short (which can also return the day name in the browser default language). – RobG Feb 08 '18 at 04:52
  • Yes. my bad. It would give date but for weekday, we could use moment().day(). This would give a number 1-7 for that particular day. – Prashanti.D Feb 22 '18 at 17:16