【问题标题】:how to create file according to date如何根据日期创建文件
【发布时间】:2013-05-08 07:34:42
【问题描述】:

我想根据数据创建文件。如果文件存在今天的日期,那么它一定不能创建文件。我已经写了一些代码

 path.exists('./LOG/BAlDate-Info' +Date()+'.txt', function(exists) {
    if (!exists) {
        fs.writeFile('./LOG/BALDate-Info' +Date()+'.txt',"BAl-Employee  is called  @  "+    Date(),function(err){
            if(err){
                console.log("error is: " + err)
            }
            else
                console.log("no error found");
        });
    }
});

在这种情况下,每次我运行此代码时,它都会创建文件,但我只想创建今天日期的单个文件。 在这种情况下请帮助我

【问题讨论】:

    标签: node.js path express fs


    【解决方案1】:

    一种选择是像这样命名您的日志文件:

    var now = new Date();
    var logfile_name = './LOG/BALDate-Info-' + now.getFullYear() + "-"+ now.getMonth() + "-" + now.getDate() +'.txt'
    

    您的文件应类似于“./LOG/BALDate-Info-2018-4-12.txt”

    【讨论】:

      猜你喜欢
      • 2019-06-24
      • 1970-01-01
      • 2014-02-13
      • 1970-01-01
      • 2022-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多