當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js Date.addDays()用法及代碼示例

date-and-time.Date.addDays()是用於操作 JS 日期和時間模塊的極簡函數集合,用於將額外的天數添加到現有日期和時間。

所需模塊:通過 npm 安裝模塊或在本地使用它。

  • 通過使用 npm。
npm install date-and-time --save
  • 通過使用 CDN 鏈接。
<script src="/path/to/date-and-time.min.js"></script>

用法:

addDays(dateObj, days)

參數:此方法采用以下參數作為參數:

  • dateobject:它是日期的字符串對象。
  • days:這是天數。

返回值:此方法返回更新的日期和時間。



範例1:

index.js


// Node.js program to demonstrate the  
// Date.addDays() method
  
// Importing module
const date = require('date-and-time')
  
// Creating object of current date and time 
// by using Date() 
const now = new Date();
  
// Adding Days to the existing date and time
// by using date.addDays() method
const value = date.addDays(now, 24);
  
// Display the result
console.log("updated date and time:" + value)

使用以下命令運行index.js文件:

node index.js

輸出:

updated date and time:
Wed Mar 31 2021 20:07:07 GMT+0530 (India Standard Time)

範例2:

index.js


// Node.js program to demonstrate the  
// Date.addDays() method
  
// Importing module
const date = require('date-and-time')
  
// Creating object of current date and time 
// by using Date() 
const now = new Date();
  
now.setDate(20)
  
// Adding Days to the existing date and time
// by using date.addDays() method
const value = date.addDays(now, 24);
  
// Display the result
console.log("updated date and time:" + value)

使用以下命令運行index.js文件:

node index.js

輸出:

updated date and time:
Tue Apr 13 2021 20:09:57 GMT+0530 (India Standard Time)

參考: https://github.com/knowledgecode/date-and-time#adddaysdateobj-days




相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js Date.addDays() API。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。