安全地获取批量当天的月份和年份

时间:2012-09-28 07:30:51

标签: windows date batch-file

我正在尝试编写一个批处理脚本,以安全的方式获取当前日期,月份和年份(即独立于日期格式)。不幸的是,我发现的所有互联网资源都提出了类似的内容:

   SET CURRENT_DATE=%DATE: =0%
   SET YEAR=%DATE:~-4%
   SET MONTH=%DATE:~3,2%
   SET DAY=%DATE:~5,2%

如果我的日期格式为yyyy / mm / dd或yyyy-mm-dd,但如果格式为yy-mm-dd或其他格式,则无效。有没有一种安全的方式来获得年,月和日?

还有一个问题 - 是否可以知道日期分隔符是什么(第一种情况为/,其他情况为-

3 个答案:

答案 0 :(得分:11)

此代码的前四行将为您提供XP Pro及更高版本中可靠的YY DD MM YYYY HH Min Sec变量。

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause

以下是VBS解决方案:

  :: date time using WSH/VBS
  :: datetime.bat V4.2
  ::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  ::
  :: This uses Windows Scripting Host to set variables to
  :: the current date/time/day/day_number/week_of_year etc
  :: for Win9x/ME/NT/W2K/XP/Vista/Win7/Win8 etc
  :: Thanks go to Todd Vargo for his scripting
  ::
  ::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  @echo off
  set TmpFile="%temp%.\tmp.vbs"
  echo> %TmpFile% n=Now
  echo>>%TmpFile% With WScript
  echo>>%TmpFile% .Echo "set m1="   + monthname(month(n), true)
  echo>>%TmpFile% .Echo "set m2="   + monthname(month(n), false)
  echo>>%TmpFile% .Echo "set woy="  + CStr(datepart("ww", n))
  echo>>%TmpFile% .Echo "set year=" + CStr(Year(n))
  echo>>%TmpFile% .Echo "set yr="   + Right(Year(n),2)
  echo>>%TmpFile% .Echo "set month="+ Right(100+Month(n),2)
  echo>>%TmpFile% .Echo "set day="  + Right(100+Day(n),2)
  echo>>%TmpFile% .Echo "set hour=" + Right(100+Hour(n),2)
  echo>>%TmpFile% .Echo "set min="  + Right(100+Minute(n),2)
  echo>>%TmpFile% .Echo "set sec="  + Right(100+Second(n),2)
  echo>>%TmpFile% .Echo "set dow="  + WeekDayName(Weekday(n),1)
  echo>>%TmpFile% .Echo "set dow2=" + WeekDayName(Weekday(n))
  echo>>%TmpFile% .Echo "set iso="  + CStr(1 + Int(n-2) mod 7)
  echo>>%TmpFile% .Echo "set iso2=" + CStr(Weekday(n,2))
  echo>>%TmpFile% End With
  cscript //nologo "%temp%.\tmp.vbs" > "%temp%.\tmp.bat"
  call "%temp%.\tmp.bat"
  del  "%temp%.\tmp.bat"
  del  %TmpFile%
  set TmpFile=
  set stamp=%year%-%month%-%day%.%hour%_%min%_%sec%

  if not "%~1"=="" goto :EOF

  echo The year  is "%year%" or "%yr%"
  echo The month is "%month%" "%m1%" "%m2%"
  echo The day   is "%day%" "%dow%" "%dow2%"
  echo.
  echo ISO8601 Day-Of-Week number is "%iso%" and week of year is: "%woy%"

  echo.
  echo The time in hh:mm:ss is "%hour%:%min%:%sec%"
  echo The hour   is "%hour%"
  echo The minute is "%min%"
  echo The second is "%sec%"
  echo.

  echo The date and time stamp is "%stamp%"
  echo.
  echo date A yyyymmdd "%year%%month%%day%"
  echo date B mmddyyyy "%month%%day%%year%"
  echo date C ddmmyyyy "%day%%month%%year%"
  echo date D yymmdd   "%yr%%month%%day%"
  echo date E mmddyy   "%month%%day%%yr%"
  echo date F ddmmyy   "%day%%month%%yr%"
  pause
  :: datetime.bat
  ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

答案 1 :(得分:8)

首先,您可以从注册表(某处)检索当前设置 格式以及分隔符。

但是使用wmic更容易,它总是采用相同的格式,唯一的缺点是,每次启动它都需要一些时间。

来自SO: Log Date/Time

的修改样本
@echo off
setlocal EnableDelayedExpansion
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
    if "%%B" NEQ "" (
        SET /A FDATE=%%F*10000+%%D*100+%%A
        SET /A FTIME=%%B*10000+%%C*100+%%E
    )
)
ECHO !FDATE! - !FTIME!

答案 2 :(得分:0)

提取日,月和年(与语言区无关)

我不会使用WMIC,因为它资源非常高,需要一段时间才能完成,尤其是在较慢的计算机上。以下脚本使用户'来自注册表的本地化,这是与语言环境无关的。据我所知,是唯一有效的批量解决方案。当然你也可以使用VBScript调用,但对我来说这是一个偏离主题的内容。

@echo off

::: Begin set date
setlocal EnableExtensions EnableDelayedExpansion

:: Determine short date format (independent from localization) from registry
for /f "skip=1 tokens=3-5 delims=- " %%L in ( '2^>nul reg query "HKCU\Control Panel\International" /v "sShortDate"' ) do ( 

    :: Since we can have multiple (short) date formats we only use the first char from the format in a new variable
    set "_L=%%L" && set "_L=!_L:~0,1!" && set "_M=%%M" && set "_M=!_M:~0,1!" && set "_N=%%N" && set "_N=!_N:~0,1!"

    :: Now assign the date values to the new vars
    for /f "tokens=2-4 delims=/-. " %%D in ( "%date%" ) do ( set "!_L!=%%D" && set "!_M!=%%E" && set "!_N!=%%F" )
)


:: Print the values as is
echo.
echo This is the original date string --^> %date%
echo These are the splitted values    --^> Day: %d%, Month:%m%, Year: %y%.
echo.

endlocal

仅提取年份

对于我写的脚本,我只想提取年份(与语言环境无关),所以我想出了这个oneliner,因为我无法找到任何解决方案。它使用' DATE' var,多个分隔符并检查大于31的数字。然后将是当前年份。与其他一些解决方案相比,它的资源较少。

@echo off

setlocal EnableExtensions

for /f " tokens=2-4 delims=-./ " %%D in ( "%date%" ) do ( if %%D gtr 31 ( set "_YEAR=%%D" ) else ( if %%E gtr 31 ( set "_YEAR=%%E" ) else ( if %%F gtr 31 ( set "_YEAR=%%F" ) ) ) )

echo And the year is... %_YEAR%.
echo.

endlocal