Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

last modified file date in node.js

Tags:

node.js

I'm trying to retrieve the last modified date of a file on the server using node.js.

I've tried

file.lastModified; 

and

file.lastModifiedDate; 

both come back as undefined.

like image 448
Fred Avatar asked Sep 26 '11 18:09

Fred


People also ask

How to get last modified date of a file in Node js?

As you can see, the mtime is the last modified time. you can use also sync version: fs. statSync(path) returns same object.

How do I find the last modified date of a file?

The lastModified() method of the File class returns the last modified time of the file/directory represented by the current File object. You can get the last modified time of a particular file using this method.

How to get the last modified date of a file in javascript?

Javascript has provided a command called document. lastModified to get the instance when the document is last modified. This command will provide the exact date and time of modification.

What is FS statSync?

The fs. statSync() method is used to synchronously return information about the given file path.


1 Answers

You should use the stat function :

According to the documentation :

fs.stat(path, [callback]) 

Asynchronous stat(2). The callback gets two arguments (err, stats) where stats is a fs.Stats object. It looks like this:

{ dev: 2049 , ino: 305352 , mode: 16877 , nlink: 12 , uid: 1000 , gid: 1000 , rdev: 0 , size: 4096 , blksize: 4096 , blocks: 8 , atime: '2009-06-29T11:11:55Z' , mtime: '2009-06-29T11:11:40Z' , ctime: '2009-06-29T11:11:40Z'  } 

As you can see, the mtime is the last modified time.

like image 172
Sandro Munda Avatar answered Oct 13 '22 04:10

Sandro Munda