以DD-MMM-YYYY格式存储日期

时间:2015-06-15 06:39:23

标签: date batch-file

我想以DD-MMM-YYYY格式(例如,12-JUN-2015)获取今天的日期,以便我可以从将每天创建的FTP服务器下载文件。

我正在使用

@echo off
set month-num=%date:~3,2%
IF "%month-num:~0,1%"=="0" SET month-num=%month-num:~1%
FOR /f "tokens=%month-num%" %%a in ("jan feb mar apr may jun jul aug sep oct nov dec") do set mo-name=%%a
echo %mo-name%

它给出了语法错误

我很感激任何建议,因为我不知道从哪里开始。

2 个答案:

答案 0 :(得分:2)

Ganesh, your use of a for /f loop to convert the numeric month to its abbreviation is pretty clever. I think part of your problem is that whoever originally wrote the code you're using wrote it for their locale, not yours. %date% looks different to the original author than it looks to you.

The format of %date% is unpredictable (well, without complicated registry queries and conversions anyway). To obtain datetime in a locale agnostic way, it's easier to use wmic os get localdatetime.

Try this:

@echo off
setlocal

for /f %%I in ('wmic os get localdatetime /format:list ^| find "="') do set "%%I"
set "YYYY=%localdatetime:~0,4%"
set /a "MM=1%localdatetime:~4,2% - 100"
set "DD=%localdatetime:~6,2%"
for /f "tokens=%MM%" %%I in ("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC") do set "month=%%I"

echo %DD%-%month%-%YYYY%

答案 1 :(得分:0)

通常会要求提供上个月的报告。因此,要获取上个月的内容,请进行以下修改:

@echo off
setlocal

for /f %%I in ('wmic os get localdatetime /format:list ^| find "="') do set "%%I"
set "YYYY=%localdatetime:~0,4%"
set /a "MM=1%localdatetime:~4,2% - 101"
set "DD=%localdatetime:~6,2%"
for /f "tokens=%MM%" %%I in ("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC") do set "month=%%I"

echo %month%-%YYYY%

现在将这个mmm-yyyy传递给您的EXE,将使其在该月份运行。