如何在 Windows 批处理文件中将短日期转换为长日期?

How to convert short date to long date in a Windows batch file?

我一直在各种站点上搜索使用 Windows 批处理文件将日期字符串(例如 Dec17 转换为 December2017Jul18 转换为 July2018。但是我找不到符合这个要求的确切命令。

如何在 Windows 批处理文件中将短日期转换为长日期?

运行 命令提示符下的批处理文件 window 不带任何参数或带有 Dec17Jul18 之类的参数以查看其输出日期。

@echo off
if "%~1" == "" (
    set "ShortDate=Jan18"
) else (
    set "ShortDate=%~1"
)

rem Get abbreviated month by using a string substitution for getting just
rem the first 3 characters from value of environment variable ShortDate.
rem First character has character index 0, 3 is the number of characters.

set "Month=%ShortDate:~0,3%"

rem Get the year by using a string substitution for getting all characters
rem from value of environment variable ShortDate starting from fourth
rem character to end of string value (number of characters not specified).
rem Century 20 is already added here.

set "Year=20%ShortDate:~3%"

if /I "%Month%" == "Jan" set "Month=January"   & goto OutputLongDate
if /I "%Month%" == "Feb" set "Month=February"  & goto OutputLongDate
if /I "%Month%" == "Mar" set "Month=March"     & goto OutputLongDate
if /I "%Month%" == "Apr" set "Month=April"     & goto OutputLongDate
if /I "%Month%" == "May" set "Month=May"       & goto OutputLongDate
if /I "%Month%" == "Jun" set "Month=June"      & goto OutputLongDate
if /I "%Month%" == "Jul" set "Month=July"      & goto OutputLongDate
if /I "%Month%" == "Aug" set "Month=August"    & goto OutputLongDate
if /I "%Month%" == "Sep" set "Month=September" & goto OutputLongDate
if /I "%Month%" == "Oct" set "Month=October"   & goto OutputLongDate
if /I "%Month%" == "Nov" set "Month=November"  & goto OutputLongDate
if /I "%Month%" == "Dec" set "Month=December"  & goto OutputLongDate

:OutputLongDate
echo %Month%%Year%

要了解使用的命令及其工作原理,请打开命令提示符 window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。

  • call /? ... 解释 %~1
  • echo /?
  • goto /?
  • if /?
  • rem /?
  • set /?

另请参阅 Single line with multiple commands using Windows batch file 以了解用于 运行 两个命令的运算符 & 的解释 – SETGOTO – 无条件地从一个命令行。

这里有一些替代想法。

以下示例使用 For 循环和延迟扩展来解析月份:

@Echo Off
SetLocal EnableDelayedExpansion
For %%A In (Long Short) Do Set "%%ADate="

Set /P "ShortDate=Please enter the date in short date format [MMMyy]: "

For %%A In (January February March April May June July August September October
    November December) Do (Set "LongMonth=%%A"
    If /I "%ShortDate:~,3%"=="!LongMonth:~,3!" (
        Set "LongDate=%%A20%ShortDate:~-2%"))

If Defined LongDate Echo %LongDate%
Pause

…这使用具有变量扩展和替换的密钥对:

@Echo Off
Set "kp=Jan-January;Feb-February;Mar-March;Apr-April;May-May;Jun-June;Jul-July"
Set "kp=%kp%;Aug-August;Sep-September;Oct-October;Nov-November;Dec-December"

Set /P "ShortDate=Please enter the date in short date format [MMMyy]: "

Call Set "LongMonth=%%kp:*%ShortDate:~,3%-=%%"
Set "LongMonth=%LongMonth:;="&:"%"
Set "LongYear=20%ShortDate:~-2%"
Set "LongDate=%LongMonth%%LongYear%"

Echo=%LongDate%
Pause

…以下示例从 PowerShell 获得帮助:

@Echo Off
Set "SDF=MMMyy"
Set "LDF=MMMMyyyy"
For %%A In (L S) Do Set "%%AD="

Set /P "SD=Please enter the date in short date format [MMMyy]: "

For /F UseBackQ %%A In (`PowerShell^
 "([datetime]::ParseExact('%SD%','%SDF%', [System.Globalization.CultureInfo]::CurrentCulture)).ToString('%LDF%')"
 `) Do Set "LD=%%A"

If Defined LD Echo %LD%
Pause