date-and-time Date.subtract() 方法用於減去兩個日期對象。
所需模塊:通過 npm 安裝模塊或在本地使用它。
- 通過使用 npm。
npm install date-and-time --save
- 通過使用 CDN 鏈接。
<script src="/path/to/date-and-time.min.js"></script>
用法:
const subtract(date1, date2)
參數:此方法將以下參數作為參數:
- date1:它是從中減去第二個日期的第一個日期對象。
- date2:它是第二個日期對象。
返回值:此方法返回從 date1 減去 date2 的結果對象。
範例1:
index.js
// Node.js program to demonstrate the
// Date.subtract() method
// Importing date-and-time module
const date = require('date-and-time')
// Creating object of current date and time
// by using Date()
const now = new Date();
// Creating object of given date and time
const date1 = new Date(2015, 0, 1);
// Subtracting the both dates
// by using date.subtract() method
const value = date.subtract(now,date1);
// Display the result
console.log("total days between them "
+ value.toDays())
使用以下命令運行index.js文件:
node index.js
輸出:
total days between them 2270.3951833333335
範例2:
index.js
// Node.js program to demonstrate the
// Date.subtract() method
// Importing date-and-time module
const date = require('date-and-time')
// Creating object of current date and time
// by using Date()
const now = new Date();
// Setting days in the exisiting date
now.setDate(20);
// Creating object of given date and time
const date1 = new Date(2015, 0, 1);
// Subtracting the both dates
// by using date.subtract() method
const value = date.subtract(now,date1);
// Display the result
console.log("total days between them " + value.toDays())
使用以下命令運行index.js文件:
node index.js
輸出:
total days between them 2270.397227997685
參考:https://github.com/knowledgecode/date-and-time#subtractdate1-date2
相關用法
- Node.js console.timeLog()用法及代碼示例
- Node.js x509.toLegacyObject()用法及代碼示例
- Node.js fs.fsyncSync()用法及代碼示例
- Node.js process.nextTick()用法及代碼示例
- Node.js GM drawLine()用法及代碼示例
- Node.js GM drawArc()用法及代碼示例
- Node.js GM drawPolyline()用法及代碼示例
- Node.js GM drawBezier()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js date-and-time Date.subtract() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。