Commonを追加する

This commit is contained in:
ry.yamafuji 2025-09-02 21:40:05 +09:00
parent 184eba34e1
commit 46b08a46b5
3 changed files with 26 additions and 0 deletions

4
examles/sampleCommon.js Normal file
View File

@ -0,0 +1,4 @@
const common = require('../src/lib/common.js');
console.log(common.formatDatetime(new Date(), 'yyyy/MM/dd hh:mm:ss'));
console.log(common.formatDatetime(new Date(), 'yyyy-MM-ddThh:mm:ss'));

View File

@ -0,0 +1,22 @@
const common = {
/**
* 日時データを指定フォーマットで文字列化する
* @param {Date} dt
* @param {String} fmt
* @returns {String}
*/
formatDatetime(dt,fmt="yyyy-MM-dd hh:mm:ss"){
const pad = (n) => n < 10 ? '0' + n : n;
const replacements = {
"yyyy": dt.getFullYear(),
"MM": pad(dt.getMonth() + 1),
"dd": pad(dt.getDate()),
"hh": pad(dt.getHours()),
"mm": pad(dt.getMinutes()),
"ss": pad(dt.getSeconds())
};
return fmt.replace(/yyyy|MM|dd|hh|mm|ss/g, (match) => replacements[match]);
}
}
module.exports = common;

0
tests/test_common.js Normal file
View File