fs.utimesSync()方法用于同步更改文件的修改和访问时间戳。可以使用数字,字符串或Date对象指定时间戳。如果时间戳不能转换为正确的数字,或者是NaN,Infinity或-Infinity,则将引发错误。
用法:
fs.utimesSync( path, atime, mtime )
参数:此方法接受上述和以下所述的三个参数:
- path:它是一个字符串,表示必须更改其时间戳的文件的路径。
- atime:它是数字,字符串或Date对象,表示要设置的新访问时间戳。
- mtime:是数字,字符串或Date对象,表示要设置的新修改时间戳。
以下示例说明了Node.js中的fs.utimesSync()方法:
范例1:
// Node.js program to demonstrate the
// fs.utimesSync() method
// Import the filesystem module
const fs = require('fs');
console.log("Details before changing time:");
// Get the stats object of the file
prevStats = fs.statSync("example_file.txt");
// Access the modified and access time of the file
console.log("Modification Time:", prevStats.mtime);
console.log("Access Time:", prevStats.atime);
// Get the current time to change the timestamps
let changedModifiedTime = new Date();
let changedAccessTime = new Date();
// Use the utimesSync() function to assign
// the new timestamps
fs.utimesSync("example_file.txt",
changedAccessTime, changedModifiedTime);
// Get the stats object of the file
console.log("\nDetails after changing time:");
// Get the stats object of the file
changedStats = fs.statSync("example_file.txt");
// Access the changed modified and access time of the file
console.log("Changed Modification Time:", changedStats.mtime);
console.log("Changed Access Time:", changedStats.atime);
输出:
Details before changing time: Modification Time:2015-12-20T19:42:00.000Z Access Time:2020-05-25T15:02:04.809Z Details after changing time: Changed Modification Time:2020-05-25T15:09:37.412Z Changed Access Time:2020-05-25T15:09:37.412Z
范例2:
// Node.js program to demonstrates the
// fs.utimesSync() method
// Import the filesystem module
const fs = require('fs');
console.log("Details before changing time:");
// Get the stats object of the file
prevStats = fs.statSync("example_file.txt");
// Access the modified and access time of the file
console.log("Modification Time:", prevStats.mtime);
console.log("Access Time:", prevStats.atime);
// Get the current time to change the timestamps
let changedModifiedTime = new Date("December 21, 2015 01:12:00");
let changedAccessTime = new Date("December 23, 2015 01:15:00");
// Use the utimesSync() function to assign
// the new timestamps
fs.utimesSync("example_file.txt",
changedAccessTime, changedModifiedTime);
// Get the stats object of the file
console.log("\nDetails after changing time:");
// Get the stats object of the file
changedStats = fs.statSync("example_file.txt");
// Access the changed modified and access time of the file
console.log("Changed Modification Time:", changedStats.mtime);
console.log("Changed Access Time:", changedStats.atime);
输出:
Details before changing time: Modification Time:2020-05-25T15:09:37.412Z Access Time:2020-05-25T15:09:37.412Z Details after changing time: Changed Modification Time:2015-12-20T19:42:00.000Z Changed Access Time:2015-12-22T19:45:00.000Z
参考: https://nodejs.org/api/fs.html#fs_fs_utimessync_path_atime_mtime
相关用法
- Node.js console.timeLog()用法及代码示例
- Node.js GM magnify()用法及代码示例
- Node.js GM drawCircle()用法及代码示例
- Node.js GM modulate()用法及代码示例
- Node.js GM drawEllipse()用法及代码示例
- Node.js GM drawPolygon()用法及代码示例
- Node.js GM drawBezier()用法及代码示例
- Node.js GM equalize()用法及代码示例
- Node.js GM enhance()用法及代码示例
- Node.js GM bordercolor()用法及代码示例
- Node.js GM transparent()用法及代码示例
- Node.js GM drawLine()用法及代码示例
- Node.js GM drawPolyline()用法及代码示例
- Node.js GM monochrome()用法及代码示例
注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 Node.js | fs.utimesSync() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。