Commonを追加する
This commit is contained in:
parent
184eba34e1
commit
46b08a46b5
4
examles/sampleCommon.js
Normal file
4
examles/sampleCommon.js
Normal 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'));
|
@ -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
0
tests/test_common.js
Normal file
Loading…
x
Reference in New Issue
Block a user