当我不添加时区时,js运行正常,但当我尝试回显巴巴多斯时区时,我得到下面与now.toutString相关的错误。我该如何解决这个问题?
未捕获的typeerror:now.ToutString不是HtmlButtoneElement的函数(索引:js:24)
const txtTrollName = document.getElementById('txtTrollName');
const txtTrollMsg = document.getElementById('txtTrollMsg');
const btnNewTrollMsg = document.getElementById('btnNewTrollMsg');
function updateTrollbox(trollPost) {
var trollData = '<p><b>' + trollPost.troll;
trollData += '</b> said: ' + trollPost.message;
trollData += ' - <small>' + trollPost.date + '</small><p>';
// inject into HTML list element
var currentTrollbox = document.getElementById('trollbox').innerHTML;
document.getElementById('trollbox').innerHTML = currentTrollbox + trollData;
}
// new trollbox entry
btnNewTrollMsg.addEventListener('click', e => {
const troll = txtTrollName.value;
const msg = txtTrollMsg.value;
// const now = new Date();
const now = new Date().toLocaleString('en-US', { timeZone: 'America/Barbados' });
const trollPost = {
troll: troll,
message: msg,
date: now.toUTCString()
};
// update posts
updateTrollbox(trollPost);
// clear textbox
document.getElementById('txtTrollName').value = '';
document.getElementById('txtTrollMsg').value = '';
console.clear();
});
1条答案
按热度按时间amrnrhlw1#
这是因为const now=new date()。您正在尝试对字符串使用toutString方法。应该对日期对象应用toutString()方法。