我正在尝试编写一个函数来显示当前时间与例如时间之间的时差。消息的创建时间。因此,我编写了一个函数来获取当前时间,然后再进行下一步。
我打算将结果放入表中<td>{getCurrentTime}</td>
,但我看不到显示此函数的任何结果。甚至没有显示第二列。
import React from "react";
export function getCurrentTime () {
let today = new Date();
let dateTime = (today.getHours() < 10 ? '0' : '') + ':' + (today.getMinutes() < 10 ? '0' : '') + ':' + (today.getSeconds() < 10 ? '0' : '');
return dateTime;
}
//console.log(getCurrentTime);
export default getCurrentTime
这是我想要显示时间的地方:
const CommitMsgTable = ({ apiCommitsResponse }: CommitMsgTableProps) => {
let colorToggle = false;
return <div><table>{apiCommitsResponse.map(commit => {
colorToggle = !colorToggle;
return (
<tr>
<td>{getCurrentTime}</td>
<td style={{ backgroundColor: colorToggle ? "lightgrey" : "white" }}>
{commit}
</td>
</tr>)
})}
</table></div>
由 tanmay 和 El Pandario 解决! 我在表数据中调用{getCurrentTime}后忘记了括号()
最佳答案
这样的事情应该有效:
export default function App() {
return (
<div className="App">
<tr>
<td>Time: {getCurrentTime()}</td>
</tr>
</div>
);
}
export function getCurrentTime () {
let today = new Date();
let dateTime = (today.getHours() < 10 ? `0${today.getHours()}` : `${today.getHours()}`) + ':' + (today.getMinutes() < 10 ? `0${today.getMinutes()}` : `${today.getMinutes()}`) + ':' + (today.getSeconds() < 10 ? `0${today.getSeconds()}` : `${today.getSeconds()}`);
console.log(dateTime);
return dateTime;
}
关于javascript - React-Typescript : I don't find the problem why my getCurrentTime Function won't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72055399/