Javascript 从图像中获取日期

Javascript 从图像中获取日期,javascript,electron,multer,Javascript,Electron,Multer,我正在构建一个用于上传图像的应用程序,当我需要获取图像上传的日期时,我遇到了问题。图像将随机上传,所以日期必须等于上传日期。 我正在和electron一起使用应用程序,对于图像上传,我正在使用multer 这是我的图片库代码,在这里我可以上传图片到图片库。如果有人知道该怎么办,我将不胜感激 // Image gallery app.get('/gallery', (req, res) => { let images = getImagesFromDir(path.join(

我正在构建一个用于上传图像的应用程序,当我需要获取图像上传的日期时,我遇到了问题。图像将随机上传,所以日期必须等于上传日期。 我正在和electron一起使用应用程序,对于图像上传,我正在使用multer

这是我的图片库代码,在这里我可以上传图片到图片库。如果有人知道该怎么办,我将不胜感激

// Image gallery
    app.get('/gallery', (req, res) => {
    let images = getImagesFromDir(path.join(__dirname, 'public/uploads'));
    let time = new Date(images.lastModified); //here I'm trying to get date
    res.render('gallery', { title: 'Galerija slik', images: images, time: time })
});

// dirPath: target image directory
function getImagesFromDir(dirPath) {

    // All iamges holder, defalut value is empty
    let allImages = [];

    // Iterator over the directory
    let files = fs.readdirSync(dirPath);

    // Iterator over the files and push jpg and png images to allImages array.
    for (file of files) {
        let fileLocation = path.join(dirPath, file);
        var stat = fs.statSync(fileLocation);
        if (stat && stat.isDirectory()) {
            getImagesFromDir(fileLocation); // process sub directories
        } else if (stat && stat.isFile() && ['.jpg', '.png'].indexOf(path.extname(fileLocation)) != -1) {
            allImages.push('uploads/'+file); // push all .jpf and .png files to all images 
        }
    }

    // return all images in array formate
    return allImages;
}
这是我的HTML代码

      <div id="content">

        <h2 class="text-center"><%= title %></h2>

    <div id="filters">
        <a href="#" id="newer" class="selected">Newer</a>
        <a href="#" id="older">Older</a>
    </div>
    <div id="posts">

        <% for(let image of images) {%>
            <article class="post">
                <figure>
                    <a href="<%= image %>" data-fancybox="1"><img src="<%= image %>" /></a>
                </figure>
                <time><%= time %></time>
        </article>
          <% } %>

    </div>

我假设您希望获得每个图像的最后修改日期

// Image gallery
    app.get('/gallery', (req, res) => {
    let images = getImagesFromDir(path.join(__dirname, 'public/uploads'));
    res.render('gallery', { title: 'Galerija slik', images: images })
});

// dirPath: target image directory
function getImagesFromDir(dirPath) {

    // All iamges holder, defalut value is empty
    let allImages = [];

    // Iterator over the directory
    let files = fs.readdirSync(dirPath);

    // Iterator over the files and push jpg and png images to allImages array.
    for (file of files) {
        let fileLocation = path.join(dirPath, file);
        var stat = fs.statSync(fileLocation);
        if (stat && stat.isDirectory()) {
            getImagesFromDir(fileLocation); // process sub directories
        } else if (stat && stat.isFile() && ['.jpg', '.png'].indexOf(path.extname(fileLocation)) != -1) {
            allImages.push({path: 'uploads/'+file, lastModifiedDate: stat.mtime}); // push all .jpf and .png files to all images 
        }
    }

    // return all images in array formate
    return allImages;
}
html应该像下面一样更新

<div id="posts"> <% for(let image of images) {%> <article class="post"> <figure> <a href="<%= image.path %>" data-fancybox="1"><img src="<%= image.path %>" /></a> </figure> <time><%= image.lastModifiedDate %></time> </article> <% } %> </div>


我试过这个,但对我不起作用。当我用这个替换旧代码时,它不会加载任何图像。这个程序的目标是显示上传的图片,并在图片下方显示上次修改的日期。更新答案。必须在每个imageWorks perfecf中使用path和lastModifiedDate属性。谢谢你,伙计。如果你在斯洛文尼亚,请联系我,我会买午餐,“完美工作”对我来说意味着很多。谢谢。我还有一个问题。现在显示日期为2017年2月14日星期二15:14:37 GMT+0100(CET)。我如何将日期改为2017年2月14日星期二