I am trying to write a function to display the difference time between current time and e.g. the creation time of a message. Herefore I wrote a function to get my current time before heading to the next step.
I intend to put the result into a table by <td>{getCurrentTime}</td>, but I cannot see any results of this function being displayed. There is not even a second column showing up.
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
this is where I want the time to be displayed:
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>
>Solution :
Something like this should work:
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;
}