簡體   English   中英

如何在不丟失原始格式的情況下將日期更改為字符串?

[英]How to change a date to a string without losing the original formatting?

我的日期是 2022-01-19T13:00:56.000Z

我需要能夠替換其中的一些字符,所以我需要將其轉換為字符串。

但是,如果我使用 toString(),它會丟失它的格式:

console.log(myDate.toString())
// output is Wed Jan 19 2022 21:00:56 GM +0800 (Hong Kong Standard Time)

我想將日期 2022-01-19T13:00:56.000Z 轉換為“2022-01-19T13:00:56.000Z”。

如何做到這一點?

toISOString 確實會生成一個您可以操作的字符串

 const date = new Date("2022-01-19T13:00:56.000Z") console.log(date.toLocaleString()) const isoString = date.toISOString() console.log(isoString) const ZPlus6 = isoString.replace("Z","+0600") console.log(ZPlus6) console.log(new Date(ZPlus6))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM