admin管理员组

文章数量:1393409

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 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?

Share Improve this question edited Apr 2, 2018 at 12:10 Alexander Lallier 4956 silver badges21 bronze badges asked Apr 2, 2018 at 9:35 James MillerJames Miller 1213 silver badges13 bronze badges 1
  • Possible duplicate of Convert a Unix timestamp to time in JavaScript – bugs Commented Apr 2, 2018 at 9:36
Add a ment  | 

2 Answers 2

Reset to default 4

to get the day number from the week use:

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

See answer for this question: Day Name from Date in JS

Solution:

var i = 0;
var data = { list: [ { dt: 1522666800 } ] };

var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; 
var dayNum = new Date(data.list[i].dt * 1000).getDay();
var result = days[dayNum];
console.log(result);

本文标签: javascriptConvert Unix Timestamp to week dayStack Overflow