如何修改上传文件的时间和日期(图文)
修改上传文件的时间和日期有多种方法,以下是常见的几种方式:
1. 使用系统命令行工具
Linux/macOS系统
使用touch 命令:
bash
# 修改文件的修改时间和访问时间 touch -m -t YYYYMMDDHHMM filename.ext # 示例:将文件时间设置为2023年12月25日14:30 touch -m -t 202312251430 document.pdf # 同时修改访问时间和修改时间 touch -a -m -t 202312251430 document.pdfWindows系统
使用 PowerShell:powershell
# 修改文件的最后写入时间 $filePath = "C:\path\to\your\file.txt" $dateTime = Get-Date "2023-12-25 14:30:00" (Get-Item $filePath).LastWriteTime = $dateTime # 修改创建时间 (Get-Item $filePath).CreationTime = $dateTime2. 编程方式修改
Python方式
python
import os import time from datetime import datetime # 方法1:使用os.utime() file_path = "document.pdf" # 设置新的时间戳 (year, month, day, hour, minute, second) new_time = time.mktime((2023, 12, 25, 14, 30, 0, 0, 0, 0)) os.utime(file_path, (new_time, new_time)) # 方法2:使用pathlib (Python 3.4+) from pathlib import Path file = Path("document.pdf") file.touch(times=(new_time, new_time))Node.js方式
javascript
const fs = require('fs'); const filePath = 'document.pdf'; const newDate = new Date('2023-12-25T14:30:00'); // 修改文件的修改时间和访问时间 fs.utimes(filePath, newDate, newDate, (err) => { if (err) throw err; console.log('文件时间已更新'); });3. 图形界面方式
Windows资源管理器
- 右键点击文件 → 属性
- 在"常规"选项卡中可以看到创建时间、修改时间
- 注意:Windows资源管理器不允许直接修改这些时间
使用第三方工具
- Attribute Changer(Windows)
- File Date Corrector(Windows)
- SetFile(macOS)
4. 批量修改多个文件
Bash脚本示例
bash
#!/bin/bash # 将目录下所有文件的时间设置为相同时间 find /path/to/directory -type f -exec touch -m -t 202312251430 {} \;Python批量处理
python
import os import time from pathlib import Path directory = "/path/to/files" new_time = time.mktime((2023, 12, 25, 14, 30, 0, 0, 0, 0)) for file_path in Path(directory).rglob('*'): if file_path.is_file(): os.utime(file_path, (new_time, new_time))注意事项
- 修改文件时间可能会影响备份软件的行为
- 某些安全软件可能会检测到此类操作
- 不同的操作系统对文件时间属性的支持略有差异
- 有些应用程序会记录自己的时间戳,与文件系统时间无关

更新时间:2025-12-15 14:28:01
