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

时间:2018-01-02 08:55:07

标签: batch-file date-formatting

我一直在各种网站上搜索使用Windows批处理文件将日期字符串Dec17转换为December2017Jul18July2018。但我无法找到符合此要求的确切命令。

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

2 个答案:

答案 0 :(得分:1)

在命令提示符窗口中运行下面的批处理文件,不带任何参数或使用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%

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

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

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

答案 1 :(得分:0)

以下是一些替代想法。

以下示例使用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