我正在使用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>任何帮助都是非常感谢的。
发布于 2016-12-09 15:24:29
您试图在时间戳上调用Moment.js的format方法,而不是瞬间的实例
您首先需要解析返回的时间戳,然后将其格式化为所需的样式。
var formatted = moment(response.dt).format("dd.mm.yyyy hh:MM:ss");假设response.dt是moment.js (文档)的解析函数支持的格式
https://stackoverflow.com/questions/41063473
复制相似问题