如何移除日期中的秒/毫秒並轉換為 ISO 字串?


首先,讓我們獲取當前日期 −

var currentDate = new Date();
console.log("The current date is as follows="+currentDate);

現在,讓我們透過使用 setSeconds() 將秒/毫秒設定為 0 來移除它們 −

currentDate.setSeconds(0,0);

使用 toISOString() 轉換為 ISO 字串 −

currentDate.toISOString()

現在,讓我們檢視帶有輸出的完整程式碼 −

示例

var currentDate = new Date();
console.log("The current date is as follows="+currentDate);
currentDate.setSeconds(0,0);
console.log("After removing seconds from date, the new date is as
follows=");
console.log(currentDate.toISOString());

若要執行以上程式,你需要使用以下命令 −

node fileName.js.

此處,我的檔名是 demo143.js。

輸出

這會生成以下輸出 −

PS C:\Users\Amit\JavaScript-code> node demo143.js
The current date is as follows=Sat Aug 01 2020 18:20:09 GMT+0530 (India Standard Time)
After removing seconds from date, the new date is as follows=
2020-08-01T12:50:00.000Z

更新日期: 2020 年 9 月 11 日

2K+ 瀏覽量

開啟你的 職業生涯

完成課程以獲得認證

開始學習
廣告
© . All rights reserved.