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) + "°</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) + "°</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
2 Answers
Reset to default 4to 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
版权声明:本文标题:javascript - Convert Unix Timestamp to week day - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744665485a2618505.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论