Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React-Typescript: I don't find the problem why my getCurrentTime Function won't work

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading