**
/*common.js供其他模块调用/
**
function getToday(){
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDate();
if (month < 10) {
month = ‘0’ + month;
};
if (day < 10) {
day = ‘0’ + day;
};
// 如果需要时分秒
// var h = now.getHours();
// var m = now.getMinutes();
// var s = now.getSeconds();
var formatDate = year + ‘-’ + month + ‘-’ + day;
return formatDate;
}
//把函数添加到对象属性里
module.exports={
getToday: getToday
}
其他模块调用
var common=require(‘common.js’);
var today=common.getToday();
本文介绍了一个简单的Node.js脚本,用于获取并格式化当前的日期。该脚本定义了一个名为`getToday`的函数,它返回格式为'YYYY-MM-DD'的当前日期。此外,还展示了如何通过`require`将此脚本作为一个模块导入到其他Node.js项目中使用。

2726

被折叠的 条评论
为什么被折叠?



