Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

reactjs - In React how to convert UTC dateTime to more readable string inside the table

I have react component which showing records in the table. I have use 'useMemo' to define table structure and data. Some of its fields are date format which showing date like '2020-12-08T07:00:00Z'. I want to convert it to more friendly reading i.e. DD:MM:YYYY :Time?

Component

const EziSchedule = () =>{

const scheduleColumns = useMemo(
    () => [
      {
        Header: "Schedule Id",
        accessor: "eziScheduleId",
      },
      {
        Header: "Start Time",
        accessor: "startTime",   //need to convert??
      },
      {
        Header: "End Time",
        accessor: "endTime",    //need to convert??
      },
    ],
    []
  );

  return (
    <div>
    <h3>Schedule</h3>
    {props.searchCriteria&& props.searchCriteria.siteId!=0 &&

    <TableItem          
        apiUrl={api.EziTrackerSchedule}
        columns={scheduleColumns}
        itemType={EcpItemTypes.EziTracker}
        customParams= {props.searchCriteria}
        selectedRow={selectedScheduleRow}
    ></TableItems>}

Error

I have tried below code in useMemo:[ ... but it throw exception

       {
        Header: "Login DateTime",
        accessor: moment("loginDateTime","DD MM YYYY hh:mm:ss"),
      },

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to output a variable with a string containing a date with momentjs you could go with moment(date).format(string). Please refer to the momentjs docs

moment().format();                                // "2014-09-08T08:02:17-05:00" (ISO 8601, no fractional seconds)
moment().format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, February 14th 2010, 3:25:50 pm"
moment().format("ddd, hA");                       // "Sun, 3PM"
moment().format("[Today is] dddd");               // "Today is Sunday"
moment('gibberish').format('YYYY MM DD');         // "Invalid date"

or with a variable

const date = "2021-01-05";
const formatted = moment(date).format(); 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...