Node.js 日期和时间 Date.isSameDay() 方法
date-and-time.Date.isSameDay()方法用于检查给定日期是否相同。
所需模块:通过 npm 安装模块或在本地使用它。
- 通过使用 npm:
npm install date-and-time --save
- 通过使用 CDN 链接:
句法:
isSameDay(date1, date2)
参数:此方法采用以下参数-
- date1:保存 date1 的 Object。
- date2:保存 date2 的 Object。
返回值:当且仅当两个日期相同时,此方法才返回布尔值 true。
示例 1:
index.js
// Node.js program to demonstrate the
// Date.isSameDay() APi
// Importing http module
const date = require('date-and-time')
// Creating object of current date
// and time by using Date()
const now1 = new Date();
// Creating object of current date
// and time using Date() method
const now2 = new Date();
// Checking if both dates are same or not
// by using date.isSameDay() api
const value = date.isSameDay(now1,now2);
// Display the result
if(value)
console.log("Both dates are same")
else
console.log("Both dates are not same")
Javascript
// Node.js program to demonstrate the
// Date.isSameDay() APi
// Importing http module
const date = require('date-and-time')
// Creating object of current date
// and time using Date() method
const now1 = new Date();
// Creating object of current date and time
// by using Date() method
const now2 = new Date();
now1.setFullYear(2016)
// Checking if both dates are same or not
// by using date.isSameDay() method
const value = date.isSameDay(now1,now2);
// Display the result
if(value)
console.log("Both dates are same")
else
console.log("Both dates are not same")
使用以下命令运行 index.js 文件:
node index.js
输出:
Both dates are same
示例 2:
Javascript
// Node.js program to demonstrate the
// Date.isSameDay() APi
// Importing http module
const date = require('date-and-time')
// Creating object of current date
// and time using Date() method
const now1 = new Date();
// Creating object of current date and time
// by using Date() method
const now2 = new Date();
now1.setFullYear(2016)
// Checking if both dates are same or not
// by using date.isSameDay() method
const value = date.isSameDay(now1,now2);
// Display the result
if(value)
console.log("Both dates are same")
else
console.log("Both dates are not same")
使用以下命令运行 index.js 文件:
node index.js
输出:
Both dates are not same
参考: https://github.com/knowledgecode/date-and-time#issamedaydate1-date2