如何在 JavaScript 中克隆 Date 物件?
在本教程中,我們將學習如何在 JavaScript 中克隆一個Date物件。我們可以建立一個新的Date類物件,該物件包含根據使用者需求的日期和時間。有時,需要複製日期,這意味著克隆它。在這種情況下,使用者可以使用下面給出的不同方法。
此外,在本教程中,我們還將學習如何在克隆的日期中設定新的時間和日期。
使用 Date.getTime() 方法克隆日期
使用者只需建立 Date 類的新的物件,並使用getTime()方法獲取從1970年1月1日開始的毫秒總數。當我們將毫秒總數作為引數傳遞給 Date() 類建構函式時,它會根據該毫秒數生成日期和時間,並返回新的日期物件。
語法
我們可以按照以下語法使用getTime()方法克隆日期。
let current_date = new Date(); let clone_Date = new Date( current_date.getTime() );
引數
current_date.getTime() - 它是從 1970 年 1 月 1 日開始的舊日期的毫秒總數。
示例
在下面的示例中,我們使用 Date 類的建構函式建立了新的日期物件。我們使用Date.getTime()方法建立了原始日期的克隆日期。
<html> <head> </head> <body> <h2> Clone a date object using JavaScript. </h2> <h4> Cloning the date using <i> Date.getTime() </i> method. </h4> <p id = "validate"> </p> <script> let validate = document.getElementById("validate");// taking the current date let current_date = new Date(); let clone_Date = new Date( current_date.getTime() ); validate.innerHTML += " The old date is : " + current_date + "<br/>"; validate.innerHTML += " The cloned date is : " + clone_Date + "<br/>"; </script> </body> </html>
在上面的輸出中,使用者可以看到舊資料和克隆日期是相同的。
使用 Date.valueOf() 方法
Date.valueOf()方法也是日期類的一種方法,它返回從第一個紀元開始的毫秒總數。我們可以使用valueOf()方法找到舊日期的毫秒總數,並將其作為引數傳遞給新的 Date 物件。
語法
使用者可以按照以下語法使用 Date.valueOf() 方法克隆日期。
let current_date = new Date(); let clone_Date = new Date( current_date.valueOf() );
示例
在下面的示例中,我們建立了新的日期物件以獲取當前日期。此外,我們還使用valueOf()方法克隆了日期,使用者可以在輸出中看到這兩個日期是相同的。
<html> <head> </head> <body> <h2> Clone a date object using JavaScript. </h2> <h4> Cloning the date using <i> Date.valueOf()</i> method. </h4> <p id = "validate"> </p> <script> let validate = document.getElementById("validate");// taking the current date let old_date = new Date(); let new_Date = new Date( old_date.valueOf() ); validate.innerHTML += " The old date is : " + old_date + "<br/>"; validate.innerHTML += " The new date is : " + new_Date + "<br/>"; </script> </body> </html>
將自定義日期設定為克隆日期
使用者已經學會了如何將日期從一個日期克隆到另一個日期並建立它的副本。現在,我們將學習如何將年份、月份或日期設定為克隆日期。我們可以簡單地使用日期類的不同方法。
使用者可以按照以下語法使用日期類的不同方法來更改克隆日期的年份、月份或日期。
語法
let date = new Date(); let new_date = new Date( date.valueOf() ); new_date.setFullYear( Year ); // set new Year new_date.setMonth( Month ); // set new Month new_date.setDate( Date ); // set new Date
引數
年份 - 它是使用者想要為克隆日期設定的新年份。
月份 - 它是克隆日期的新月份,以字串格式表示,範圍從 0 到 11。
日期 - 它設定月份中的某一天。
示例
在下面的示例中,我們克隆了日期並將新年份、月份和日期設定為克隆日期。使用者可以檢視設定新值後克隆資料和新日期之間的區別。
<html> <head> </head> <body> <h2> Clone a date object using JavaScript. </h2> <h4> Change the date and time in cloned date using <i> Date class </i> methods. </h4> <p id = "validate"> </p> <script> let validate = document.getElementById("validate"); // taking the current date let current_date = new Date(); let clone_Date = new Date(current_date.valueOf()); validate.innerHTML += " The old date is : " + current_date + "<br/>"; validate.innerHTML += " The cloned date is : " + clone_Date + "<br/>"; clone_Date.setFullYear("2021"); clone_Date.setMonth("02"); clone_Date.setDate("3"); validate.innerHTML += " After settings the custom year, month, and date to cloned date is : " + clone_Date + "<br/>"; </script> </body> </html>
在本教程中,我們學習瞭如何使用 Date 類的 getTime() 和 valueOf() 方法克隆日期。此外,我們還在教程的最後一部分學習瞭如何為日期設定新值。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP