如何在文件系统中获取最后修改日期和时间?

How to get last modified date and time in Filesystem?

您好,我正在尝试获取文件的最后修改日期和时间,该文件存储在我的本地设备内存目录中。是否可以在文件系统中获取 lastmodifiedate?

到目前为止,我一直在尝试使用以下代码

 function checkLastModifiedDate(){
 window.resolveLocalFileSystemURL(cordova.file.
    externalRootDirectory 
    +"Download/SMU/File Repo/SMUDE_Connect_User_Guide.pdf",  
        function(fs) {
            LastModifiedDateOfFile(fs);
    }, failFiles);  

function failFiles(error) {
    if (error.code == FileError.NOT_FOUND_ERR)
        toastr.error('User guide manual not exists.', 'Information')
    else if (error.code == FileError.SECURITY_ERR)
        console.log("Message : SECURITY_ERR")
    else if (error.code == FileError.ABORT_ERR)
        console.log("Message : ABORT_ERR")
    else if (error.code == FileError.NOT_READABLE_ERR)
        console.log("Message : NOT_READABLE_ERR")
    else if (error.code == FileError.ENCODING_ERR)
        console.log("Message : ENCODING_ERR")
    else if (error.code == FileError.NO_MODIFICATION_ALLOWED_ERR)
        console.log("Message : NO_MODIFICATION_ALLOWED_ERR")
    else if (error.code == FileError.INVALID_STATE_ERR)
        console.log("Message : INVALID_STATE_ERR")
    else if (error.code == FileError.SYNTAX_ERR)
        console.log("Message : SYNTAX_ERR")
    else if (error.code == FileError.INVALID_MODIFICATION_ERR)
        console.log("Message :  INVALID_MODIFICATION_ERR")
    else if (error.code == FileError.QUOTA_EXCEEDED_ERR)
        console.log("Message : QUOTA_EXCEEDED_ERR")
    else if (error.code == FileError.PATH_EXISTS_ERR)
        console.log("Message : PATH_EXISTS_ERR")
}
}

function LastModifiedDateOfFile(fileEntry){
var lastMod = fileEntry.lastModifiedDate;
//alert("lastMod - "+lastMod);
//var date = new Date(fileEntry.lastModified);
//alert("date - "+date);
}

参考链接http://www.html5rocks.com/en/tutorials/file/filesystem/

这样我们可以做到,但我不是通过输入标签选择文件 http://jsbin.com/ajepef/1/edit?html,output

每次都显示 'undefined',我不确定 lastmodifeddate 属性是否存在。 如果有人知道请帮助我。

您必须使用 fileEntryfile() 方法。按以下方式更改您的 LastModifiedDateOfFile 函数:

function LastModifiedDateOfFile(fileEntry){
    fileEntry.file(successFile,errorFile);

    function successFile(entry){
        alert(entry.lastModified);
    }

    function errorFile(error){
        alert("error");
    }
}

试试这个:

window.resolveLocalFileSystemURL(cordova.file.applicationDirectory + "YOUR_FILE_NAME", gotFile, fail);

function fail(e) {
    console.log("FileSystem Error");
    console.dir(e);
}

function gotFile(fileEntry) {
    fileEntry.file(function(file) {
        console.log(file.lastModifiedDate);        
    });
}

希望对您有所帮助