Search code examples
javascripthtmlunix-timestampdayofweekopenweathermap

Convert Unix Timestamp to week day


I know here is a similar question but I could not apply this to my code. Don't know where to put it and different result needed.

success: function(data){
            
  var table = '';
  
  var header = '<h2>' + data.city.name + ', ' + data.city.country + '</h2>'
  
  for(var i = 0; i < data.list.length; i++){
      table += "<tr>";
      
      table += "<td>" + data.list[i].dt + "</td>";
      table += "<td><img src='img/"+data.list[i].weather[0].description+".svg'></td>";
      table += "<td>" +data.list[i].weather[0].description+ "</td>";
      table += "<td>" + Math.round(data.list[i].temp.day) + "&deg;</td>";           
      
      table += "</tr>";
  }
  
  $("#forecastWeather").html(table);
  $("#header").html(header);
  
  $("#city").val('');
  }
});

I am making a weather app using Open Weather Map Forecast 16. All works fine but I cant figure out how to convert the unix timestamp in my code to the day of the week. Here is the line in my JavaScript code where I add the date to the table:

table += "<td>" + data.list[i].dt + "</td>";

This gives my number like: 1522666800. How do I get it to show a weekday?


Solution

  • to get the day number from the week use:

    new Date(data.list[i].dt * 1000).getDay()