Node js get the last modified Date of a File

Node js get the last modified Date of a File

To get last modified date of file in node.js; In this tutorial, you will learn how to get the last modified date time of file in node js using fs module, fs.promises.stat, util.promisify method.

Node js get the last modified Date of a File

Here are some methods to get last modified date time of file in node.js using fs module, fs.promises.stat, util.promisify method:

  • Method 1: Using fs.stat
  • Method 2: Using fs.promises.stat (Node.js 10+)
  • Method 3: Using the util.promisify method (Node.js 8+)

Method 1: Using fs.stat

Using the fs.stat method, you can get detailed information about a file. Here’s is example to get last modified date of file using fs.stat:

const fs = require('fs');

const filePath = 'path/to/your/file.txt'; // Replace with your file's path

fs.stat(filePath, (err, stats) => {
  if (err) {
    console.error('Error:', err);
    return;
  }

  const lastModified = stats.mtime; // mtime is the last modified date
  console.log('Last modified:', lastModified);
});

Method 2: Using fs.promises.stat (Node.js 10+)

If you prefer working with Promises, you can use the fs.promises.stat method, available in Node.js versions 10 and above:

const fs = require('fs').promises;

const filePath = 'path/to/your/file.txt';

(async () => {
  try {
    const stats = await fs.stat(filePath);
    const lastModifiedDate = stats.mtime; // Get the last modified date
    console.log(`Last modified date: ${lastModifiedDate}`);
  } catch (err) {
    console.error(err);
  }
})();

Method 3: Using the util.promisify method (Node.js 8+)

For Node.js versions 8 and above, you can also use the util.promisify method to convert the callback-based fs.stat function into a Promise-based one:

const fs = require('fs');
const util = require('util');

const stat = util.promisify(fs.stat);
const filePath = 'path/to/your/file.txt';

(async () => {
  try {
    const stats = await stat(filePath);
    const lastModifiedDate = stats.mtime; // Get the last modified date
    console.log(`Last modified date: ${lastModifiedDate}`);
  } catch (err) {
    console.error(err);
  }
})();

In all of these methods, stats.mtime gives you the last modified date as a JavaScript Date object, which you can format or manipulate as needed. You can also use other properties of the stats object to access information like file size, permissions, and more.

Certainly! stats.mtime is a property in Node.js that represents the last modification time of a file. It returns a JavaScript Date object, which you can use to obtain various details about the file’s last modification date and time.

Here’s an example of how to access and display information from stats.mtime:

const fs = require('fs');

const filePath = 'path/to/your/file.txt';

fs.stat(filePath, (err, stats) => {
  if (err) {
    console.error(err);
    return;
  }

  const lastModifiedDate = stats.mtime; // Get the last modified date
  console.log(`Last modified date: ${lastModifiedDate}`);
  
  // Access specific information about the last modification date
  const year = lastModifiedDate.getFullYear();
  const month = lastModifiedDate.getMonth() + 1; // Month is zero-based, so add 1
  const day = lastModifiedDate.getDate();
  const hours = lastModifiedDate.getHours();
  const minutes = lastModifiedDate.getMinutes();
  const seconds = lastModifiedDate.getSeconds();

  console.log(`Last modified on ${year}-${month}-${day} at ${hours}:${minutes}:${seconds}`);
});

Remember to replace 'path/to/your/file.txt' with the actual path to the file you want to check.

Conclusion

That’s it; you have learned how to get the last modified date time of file in node js using fs module, fs.promises.stat, util.promisify method.

Recommended Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *