可以使用 JavaScript 的 Date 对象来获取当前时间,然后将时间作为文件名的一部分,最后将文件保存在指定路径下。

以下是一个示例代码:

// 获取当前时间
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();

// 构造文件名
var filename = "file_" + year + "-" + month + "-" + day + "_" + hour + "-" + minute + "-" + second + ".js";

// 保存文件
var fileContent = "console.log('Hello, World!')";
var filePath = "path/to/save/" + filename;

// 使用文件系统模块保存文件
var fs = require('fs');
fs.writeFile(filePath, fileContent, function(err) {
    if (err) {
        console.error(err);
        return;
    }
    console.log(filename + " saved successfully!");
});

这段代码会生成一个类似于 file_2021-10-19_12-30-45.js 的文件名,并将文件保存在指定路径下。你可以根据自己的需要修改文件名格式和保存路径。

JS 文件保存时加上时间

原文地址: http://www.cveoy.top/t/topic/bJhL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录