【发布时间】:2017-04-25 02:42:00
【问题描述】:
我正在使用OpenWeatherMap API,我正在尝试检索LastUpdated 属性。
让我换个说法,我可以检索 LastUpdated 属性,但对于 JSON 格式,它以时间戳的形式提供。
如何将该时间戳转换为正常的日期/时间以显示给用户?
我做了一些研究并下载了 Moment.js,但是当我运行该页面时,我收到一条错误消息:
错误:对象不支持属性或方法“格式”
这是我的代码:
<script src="~/Scripts/moment.js"></script>
<script>
$(document).ready(function () {
var request = $.ajax({
url: "http://api.openweathermap.org/data/2.5/weather",
method: "GET",
data: { id: '4142290', appid: 'myapikey', units: 'imperial' },
success: function (response) {
console.log(response);
var weatherIcon = response.weather[0].icon;
var weatherDescription = response.weather[0].description;
var weatherTemp = response.main.temp;
var lastUpdated = response.dt;
var formatted = lastUpdated.format("dd.mm.yyyy hh:MM:ss");
var iconUrl = "http://openweathermap.org/img/w/" + weatherIcon + ".png";
document.getElementById('Weather-Image').src = iconUrl;
document.getElementById('Weather-Description').innerHTML = "[" + weatherDescription + "]";
document.getElementById('Weather-Temp').innerHTML = weatherTemp;
document.getElementById('Weather-Updated').innerHTML = formatted;
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.responseText)
}
});
});
</script>
感谢任何帮助。
【问题讨论】:
-
查看:stackoverflow.com/questions/19485353/…,最佳解决方案由 RobG 给出,获得了 41 个赞
标签: javascript datetime timestamp