我只想填充日期时间的时间 (HH:mm)。我尝试了一些方法,但我不断收到“未捕获的类型错误:未定义不是函数”。 我的 ajax GET 调用工作正常。
var id = 0;
var URL = "http://localhost:58510/api/requests/";
onload = function() {
showRequestDetails();
};
function showRequestDetails() {
var query = window.location.search.substring(1);
var value = query.split("=");
id = value[1];
jQuery.support.cors = true;
$.ajax({
url: URL + id,
type: 'GET',
dataType: 'json',
success: function(data, textStatus, xhr) {
$("#ddArr_txt").val(dateToTime(data.departure_arrivaltime));
},
error: function(xhr, textStatus, errorThrown) {
console.log('Request by ID failed.');
}
});
};
function dateToTime(d) {
if (d != null) {
var timeOnly = (d.getHours() + ":" + d.getMinutes());
return timeOnly;
}
};
GET 响应示例:
<Request>
<ID>2</ID>
<departure_arrivaltime>2014-10-29T12:00:00</departure_arrivaltime>
</Request>
如何只获取输入字段中的时间?
最佳答案
我认为您的data.departure_arrivaltime
是一个字符串,请尝试在调用getHours
或getMinutes
之前将其转换为日期 -
var d = new Date(data.departure_arrivaltime);
关于Javascript日期时间时间只给出 "Uncaught TypeError: undefined is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26738522/