Node.js 日期和时间 Date.isLeapYeart() 方法
date-and-time.Date.isLeapYear()是用于操作 JS 日期和时间模块的极简函数集合,用于检查给定年份是否为闰年。
所需模块:通过 npm 安装模块或在本地使用它。
通过使用 npm:
npm install date-and-time --save
通过使用 CDN 链接:
句法:
const isLeapYear(y)
参数:此方法需要一个年份字符串。
返回值:当且仅当年份字符串为闰时,此方法才返回布尔值 true。
示例 1:
index.js
// Node.js program to demonstrate the
// Date.isLeapYear() method
// Importing http module
const date = require('date-and-time')
// creating object of current date and time
// by using Date()
const now = new Date();
// Checking the year is leap or not
// by using date.isLeapYear() api
const value = date.isLeapYear(now.getFullYear());
// display the result
if(value)
console.log("This is a leap year")
else
console.log("This is not a leap year")
index.js
// Node.js program to demonstrate the
// Date.isLeapYear() method
// Importing http module
const date = require('date-and-time')
// Creating object of current date and time
// by using Date() method
const now = new Date();
now.setFullYear(2016)
// Checking the year leap or not
// by using date.isLeapYear() api
const value = date.isLeapYear(now.getFullYear());
// Display the result
if(value)
console.log("This is a leap year")
else
console.log("This is not a leap year")
使用以下命令运行index.js文件:
node index.js
输出:
This is not a leap year
示例 2:
index.js
// Node.js program to demonstrate the
// Date.isLeapYear() method
// Importing http module
const date = require('date-and-time')
// Creating object of current date and time
// by using Date() method
const now = new Date();
now.setFullYear(2016)
// Checking the year leap or not
// by using date.isLeapYear() api
const value = date.isLeapYear(now.getFullYear());
// Display the result
if(value)
console.log("This is a leap year")
else
console.log("This is not a leap year")
使用以下命令运行index.js文件:
node index.js
输出:
This is a leap year
参考: https://github.com/knowledgecode/date-and-time#isleapyeary