[英]In React how to convert UTC dateTime to more readable string inside the table
我有反應組件,它顯示表中的記錄。 我已經使用“useMemo”來定義表結構和數據。 它的一些字段是日期格式,顯示日期如“2020-12-08T07:00:00Z”。 我想將其轉換為更友好的閱讀方式,即 DD:MM:YYYY:Time?
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>}
我在 useMemo 中嘗試過以下代碼:[... 但它拋出異常
{
Header: "Login DateTime",
accessor: moment("loginDateTime","DD MM YYYY hh:mm:ss"),
},
我無法評論,所以...
嘗試這個:
var d = new Date(YOUR DATE HERE);
var n = d.toLocaleString(CODE YOUR COUNTRY);
所以,將是這樣的:
var d = new Date('2020-12-08T07:00:00Z');
var n = d.toLocaleString('pt-BR');
結果將是: 08/12/2020 04:00:00
來源(這里有所有國家代碼): https://www.w3schools.com/jsref/jsref_tolocalestring.asp
如果你想 output 一個變量,其字符串包含一個帶有 momentjs 的日期,你可以 go 和 moment(date).format(string)。 請參考momentjs 文檔
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"
或帶有變量
const date = "2021-01-05";
const formatted = moment(date).format();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.