如何在不包装文本的情况下将 "space" 添加到输出中(React,打字稿)
How can I add "space" to the output without wrapping the text (React, typescript)
我想在 table 列中将当前时间显示为“hh:mm Uhr”。
在 hh:mm 和单词 'Uhr' 之间的代码中添加 space 之前,它运行良好。如果我这样做,文本将换行。
在我看来,space 字符在代码中起到输入的作用。
谁有适合我的可行解决方案?
这是我的代码:
let currentTime = `${hours.toString().length === 1 ? `0${hours}` : hours}:${minutes.toString().length === 1 ? `0${minutes}` : minutes} Uhr`;
我说的这段代码最后是:minutes} Uhr`;
如果我省略 space 的输出:
09:33Uhr
如果我把 space 放在那里的输出:
09:33
Uhr
您可以尝试添加
因此您的代码将是:
让当前时间 = ${hours.toString().length === 1 ?
0${小时}: hours}:${minutes.toString().length === 1 ?
0${分钟} : minutes} Uhr
;
我用不同的方法解决了这个问题。有一个插件叫dayjs。它使使用时间和编辑它变得容易得多。
现在这是我的代码:
function processDate(date: string) {
const parsedDate = dayjs(date).locale("de"); {/*Enter your country letters here to use your local time*/}
return (
<>
<div>
{parsedDate.format('DD MMM YYYY')} {/Output: *01 Jan 1970*/}
</div>
<div>
{parsedDate.format('HH:mm')} h {/Output: *00:00 h*/}
</div>
<div>
{parsedDate.from(dayjs())} {/*takes the difference from then to now*/}
</div>
</>
)
/*format('DD MMM YYYY, HH:mm [h]');*/
你必须安装dayjs才能使用它。
此外,还有一个很好的文档:
https://github.com/iamkun/dayjs/blob/dev/docs/en/API-reference.md#displaying
我想在 table 列中将当前时间显示为“hh:mm Uhr”。 在 hh:mm 和单词 'Uhr' 之间的代码中添加 space 之前,它运行良好。如果我这样做,文本将换行。 在我看来,space 字符在代码中起到输入的作用。 谁有适合我的可行解决方案?
这是我的代码:
let currentTime = `${hours.toString().length === 1 ? `0${hours}` : hours}:${minutes.toString().length === 1 ? `0${minutes}` : minutes} Uhr`;
我说的这段代码最后是:minutes} Uhr`;
如果我省略 space 的输出:
09:33Uhr
如果我把 space 放在那里的输出:
09:33
Uhr
您可以尝试添加
因此您的代码将是:
让当前时间 = ${hours.toString().length === 1 ?
0${小时}: hours}:${minutes.toString().length === 1 ?
0${分钟} : minutes} Uhr
;
我用不同的方法解决了这个问题。有一个插件叫dayjs。它使使用时间和编辑它变得容易得多。
现在这是我的代码:
function processDate(date: string) {
const parsedDate = dayjs(date).locale("de"); {/*Enter your country letters here to use your local time*/}
return (
<>
<div>
{parsedDate.format('DD MMM YYYY')} {/Output: *01 Jan 1970*/}
</div>
<div>
{parsedDate.format('HH:mm')} h {/Output: *00:00 h*/}
</div>
<div>
{parsedDate.from(dayjs())} {/*takes the difference from then to now*/}
</div>
</>
)
/*format('DD MMM YYYY, HH:mm [h]');*/
你必须安装dayjs才能使用它。 此外,还有一个很好的文档: https://github.com/iamkun/dayjs/blob/dev/docs/en/API-reference.md#displaying