我试图编写一个函数来显示当前时间与消息创建时间之间的差异时间。因此,在进入下一步之前,我编写了一个函数来获得当前的时间。
我打算通过<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>由坦梅和埃尔·潘达里奥解决!在表数据中调用{getCurrentTime}后,忘记了方括号()
发布于 2022-04-29 09:48:39
像这样的事情应该有效:
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;
}发布于 2022-04-29 09:14:57
我不明白你的意思,但我想你想要这样的东西:
const getCurrentTime = () => {
const date = new Date();
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
const timeString = `${hours.toString().length === 1 ? `0${hours}` : hours}:${minutes.toString().length === 1 ? `0${minutes}` : minutes}:${seconds.toString().length === 1 ? `0${seconds}` : seconds}`;
return timeString;
};
console.log(getCurrentTime())
https://stackoverflow.com/questions/72055399
复制相似问题