首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Windows批处理文件中将短日期转换为长日期?

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

Stack Overflow用户
提问于 2018-01-02 08:55:07
回答 2查看 574关注 0票数 1

我一直在搜索各种站点,以便使用Windows文件将日期字符串(如Dec17 )转换为December2017,将Jul18转换为July2018。但我无法找到完全符合这一要求的命令。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-02 09:56:17

在命令提示符窗口中运行下面的批处理文件,不带任何参数,或者使用Dec17Jul18这样的参数,以查看其在不同的短日期上的输出。

代码语言:javascript
复制
@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了解用于从一个命令行无条件运行两个命令( SETGOTO )的操作符&的说明。

票数 1
EN

Stack Overflow用户

发布于 2018-01-02 16:00:10

以下是一些可供选择的想法。

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

代码语言:javascript
复制
@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

…这使用了具有可变展开和替换的密钥对:

代码语言:javascript
复制
@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获得帮助:

代码语言:javascript
复制
@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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48058054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档