fsPromises.utimes()方法用于异步更改文件的修改和访问时间戳。可以使用数字,字符串或Date对象指定时间戳。如果时间戳不能转换为正确的数字,或者是NaN,Infinity或-Infinity,则将引发错误。
它更改path引用的对象的文件系统时间戳,然后在成功时不带任何参数地解决Promise。
用法:
fsPromises.utimes( path, atime, mtime )
参数:此方法接受上述和以下所述的三个参数:
- path:它是一个字符串,表示必须更改其时间戳的文件的路径。
 - atime:它是数字,字符串或Date对象,表示要设置的新访问时间戳。
 - mtime:是数字,字符串或Date对象,表示要设置的新修改时间戳。
 
atime和mtime参数遵循以下规则:
- 值可以是代表Unix纪元时间的数字,日期,也可以是数字字符串,例如“ 123456789.0”。
 - 如果该值不能转换为数字,或者是NaN,Infinity或-Infinity,则将引发错误。
 
例:本示例说明Node.js中的fsPromises.utimes()方法。创建一个example_gfg.txt文件进行输入。
文件名:index.js
// Node.js program to demonstrate the  
// fsPromises.utimes() method  
  
// Import the filesystem module  
const fs = require('fs'); 
const fsPromises = require('fs').promises; 
  
console.log("Details before changing time:"); 
  
// Get the stats object of the file  
prevStats = fs.statSync("example_gfg.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 newModifiedTime = new Date(); 
let newAccessTime = new Date(); 
  
// Use the utimes() function to assign  
// the new timestamps  
fsPromises.utimes("example_file.txt",  
        newAccessTime, newModifiedTime); 
  
// Get the stats object of the file  
console.log("\nDetails after changing time:"); 
  
// Get the stats object of the file  
changedStats = fs.statSync("example_gfg.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);运行此程序的步骤:使用以下命令运行index.js文件:
node index.js
输出:
Details before changing time: Modification Time:2020-06-11T17:25:51.136Z Access Time:2020-06-11T16:50:51.223Z Details after changing time: Changed Modification Time:2020-06-11T17:25:51.136Z Changed Access Time:2020-06-11T16:50:51.223Z
参考: https://nodejs.org/api/fs.html#fs_fspromises_utimes_path_atime_mtime
相关用法
- Node.js console.timeLog()用法及代码示例
 - Node.js GM negative()用法及代码示例
 - Node.js GM contrast()用法及代码示例
 - Node.js GM whitePoint()用法及代码示例
 - Node.js GM median()用法及代码示例
 - Node.js GM gaussian()用法及代码示例
 - Node.js GM crop()用法及代码示例
 - Node.js GM despeckle()用法及代码示例
 - Node.js GM whiteThreshold()用法及代码示例
 - Node.js GM write()用法及代码示例
 - Node.js GM gamma()用法及代码示例
 - Node.js GM roll()用法及代码示例
 - Node.js GM segment()用法及代码示例
 - Node.js GM quality()用法及代码示例
 - Node.js GM raise()用法及代码示例
 
注:本文由纯净天空筛选整理自nitin_sharma大神的英文原创作品 Node.js | fsPromises.utimes() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
