怎么更改文档时间编辑时间和日期(图文)
更改文档的时间、编辑时间和日期通常涉及修改文件的三种时间戳属性。以下是详细的解决方案:
Windows系统
使用PowerShell(推荐)
powershell
# 定义文件路径和新时间 $filePath = "C:\path\to\your\document.txt" $newDateTime = Get-Date "2023-12-25 14:30:00" # 获取文件对象 $file = Get-Item $filePath # 修改三种时间戳 $file.CreationTime = $newDateTime # 创建时间 $file.LastWriteTime = $newDateTime # 修改时间(编辑时间) $file.LastAccessTime = $newDateTime # 访问时间 # 分别修改不同时间戳 $file.LastWriteTime = Get-Date "2023-11-20 10:15:00" # 最后编辑时间 $file.CreationTime = Get-Date "2023-10-01 09:00:00" # 创建时间使用CMD命令
cmd
# 需要第三方工具,Windows自带功能有限 # 可以使用如 NirCmd 等工具 nircmd.exe setfiletime "document.txt" "25-12-2023 14:30:00" "25-12-2023 14:30:00"Linux/macOS系统
使用touch命令
bash
# 同时修改访问时间和修改时间 touch -a -m -t 202312251430.00 filename.txt # 分别修改: # -a : 修改访问时间 # -m : 修改修改时间(即编辑时间) # -t : 指定时间格式 [YY]YYMMDDhhmm[.ss] # 使用-d参数更直观地指定时间 touch -a -m -d "2023-12-25 14:30:00" filename.txt # 只修改修改时间(编辑时间) touch -m -d "2023-12-25 14:30:00" filename.txt # 只修改访问时间 touch -a -d "2023-12-25 14:30:00" filename.txt跨平台编程解决方案
Python方法
python
import os import time from datetime import datetime def modify_file_times(filepath, access_time=None, modify_time=None, create_time=None): """ 修改文件的各种时间戳 参数: filepath: 文件路径 access_time: 访问时间 modify_time: 修改时间(编辑时间) create_time: 创建时间(Windows only) """ # 如果没有指定时间,默认使用当前时间 if access_time is None and modify_time is None: # 直接更新为当前时间 os.utime(filepath, None) return # 转换时间格式 atime = time.mktime(datetime.strptime(access_time, "%Y-%m-%d %H:%M:%S").timetuple()) if access_time else None mtime = time.mktime(datetime.strptime(modify_time, "%Y-%m-%d %H:%M:%S").timetuple()) if modify_time else None # 设置时间戳 if atime is not None and mtime is not None: os.utime(filepath, (atime, mtime)) elif atime is not None: stat = os.stat(filepath) os.utime(filepath, (atime, stat.st_mtime)) elif mtime is not None: stat = os.stat(filepath) os.utime(filepath, (stat.st_atime, mtime)) # 使用示例 modify_file_times( "document.txt", access_time="2023-12-25 14:30:00", modify_time="2023-12-25 14:30:00" )批量处理多个文件
PowerShell批量修改
powershell
# 批量修改文件夹中所有文档的时间 $folderPath = "C:\Documents" $documents = Get-ChildItem $folderPath -Include *.docx,*.pdf,*.txt -Recurse $newDateTime = Get-Date "2023-12-25 14:30:00" foreach ($doc in $documents) { $doc.LastWriteTime = $newDateTime # 编辑时间 $doc.CreationTime = $newDateTime # 创建时间 $doc.LastAccessTime = $newDateTime # 访问时间 }Linux/macOS批量修改
bash
# 修改当前目录下所有文档的编辑时间 find . \( -name "*.txt" -o -name "*.pdf" -o -name "*.docx" \) -exec touch -m -d "2023-12-25 14:30:00" {} \; # 或者使用循环 for file in *.txt *.pdf *.docx; do [ -f "$file" ] && touch -m -d "2023-12-25 14:30:00" "$file" done特殊注意事项
- 权限要求:确保您对目标文件有写入权限
- 时间格式一致性:不同系统支持的时间格式可能不同
- 创建时间限制:在Linux系统中,创建时间通常不能直接修改
- 云同步影响:某些云存储服务可能会重置文件时间戳
- 备份重要文件:修改前建议备份关键文档

更新时间:2025-12-18 10:35:34
上一篇:如何修改文件修改日期(图文)
