- @echo off
-
- echo, & echo ------------ 输出默认格式
- call :Now
-
- echo, & echo ------------ 输出自定义格式
- call :Now YYYY年-MM月-DD日 HH时:MI分:SS秒 星期Week
-
- echo, & echo ------------ 设置到变量
- call :Now (set)
- set now
-
- pause
- exit /b
-
- ::=============================================================
- :: 函数: Now --- 获取当前时间和日期
- ::
- :: 原理:
- :: 从 makecab 输出文件中的第一行读取时间日期。第一行格式为:
- :: MakeCAB Report: Sat Nov 16 02:36:02 2013
- :: 参数:
- :: 默认值为 YYYY-MM-DD HH:MI:SS Week
- ::
- :: 如果参数为 "(set)",则时间日期会被设置到以下变量中:
- :: now.yyyy now.mm now.dd now.hh now.mi now.ss now.week
-
- :Now [Output=YYYY-MM-DD HH:MI:SS Week]
- makecab /d RptFileName="%temp%\.rpt" /d InfFileName=nul /f nul >nul
-
- for /f "usebackq tokens=3-9 delims=: " %%1 in ("%temp%\.rpt") do (
- setlocal enabledelayedexpansion
-
- set WeekList=Mon_1 Tue_2 Wed_3 Thu_4 Fri_5 Sat_6 Sun_7
- set /a Week = !WeekList:*%%1_=! 2>nul
-
- set MonthList=Jan_1 Feb_2 Mar_3 Apr_4 May_5 Jun_6 Jul_7 Aug_8 Sep_9 Oct_10 Nov_11 Dec_12
- set /a MM = !MonthList:*%%2_=! 2>nul
-
- if "%~1" == "" (
- echo %%7-!MM!-%%3 %%4:%%5:%%6 !Week!
- ) else if /i "%~1" == "(set)" (
- for /f "tokens=1-7" %%a in ("%%7 !MM! %%3 %%4 %%5 %%6 !Week!") do (
- endlocal
-
- set now.yyyy=%%a
- set now.mm=%%b
- set now.dd=%%c
- set now.hh=%%d
- set now.mi=%%e
- set now.ss=%%f
- set now.week=%%g
-
- goto :eof
- )
- ) else (
- set "Output=%*"
-
- set Output=!Output:YYYY=%%7!
- for %%m in (!MM!) do set Output=!Output:MM=%%m!
- for %%w in (!Week!) do set Output=!Output:Week=%%w!
- set Output=!Output:DD=%%3!
- set Output=!Output:HH=%%4!
- set Output=!Output:MI=%%5!
- set Output=!Output:SS=%%6!
-
- echo,!Output!
- )
-
- endlocal
- goto :eof
- )
- ::=============================================================
复制代码 从以下链接看到的方法:
http://with-love-from-siberia.blogspot.com/2009/11/cross-platform-datetime-definition.html |