原 Windows的cmd获取年月日时分秒的日期时间
命令1
要修改代码页,否则可能输出的时间格式不对:
1 2 3 | chcp 65001 echo %date:~3,4%%date:~8,2%%date:~11,2%%time:~0,2%%time:~3,2%%time:~6,2% |
结果示例:20240803104611
1 2 3 4 5 | set currentDateTime=%date:~3,4%%date:~8,2%%date:~11,2%%time:~0,2%%time:~3,2%%time:~6,2% set "currentDateTime=%currentDateTime: =0%" D:\PostgreSQL\16\bin\pg_dumpall --file=pg_%currentDateTime%.bk |
命令2
1 | wmic os get localdatetime | find "." |
需要注意获取小时的空格问题
1 2 3 4 | time 01:02:03 set currentDateTime=%date:~3,4%%date:~8,2%%date:~11,2%%time:~0,2%%time:~3,2%%time:~6,2% set "currentDateTime=%currentDateTime: =0%" echo %currentDateTime% |