Node.js date-and-time Date.isLeapYear() 方法
date-and-time.Date.isLeapYear() 是一个用于操作JS日期和时间模块的最简集合,用于检查给定的年份是否为闰年。
所需模块: 通过npm安装模块或本地使用。
通过npm使用:
npm install date-and-time --save
通过使用CDN链接:
<script src="/path/to/date-and-time.min.js"></script>
语法:
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 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