源码
该代码片段实现了将文件重命名为格式202307011034788.png(年月日时分毫秒.ext)
const fs = require('fs');
const date = new Date();
const addPrefix = (time) => {
if (time.length == 1) {
return '00' + time;
}
if (time.length == 2) {
return '0' + time;
}
return time;
}
const add = (time) => {
if (time.length == 1) {
return '0' + time;
}
return time;
}
const year = date.getFullYear().toString();
const month = (date.getMonth() + 1).toString();
const day = date.getDate().toString();
const hour = date.getHours().toString();
const mins = date.getMinutes().toString();
const sec = date.getMilliseconds().toString();
const res = year + add(month) + add(day) + add(hour) + add(mins) + addPrefix(sec);
const filePath = process.argv[2];
fs.rename(filePath, res + '.png', (err, res) => {
console.log(err);
});