在批处理文件拆分%DATE%,无论区域设置 [英] Split %date% in a batch file regardless of Regional Settings

查看:337
本文介绍了在批处理文件拆分%DATE%,无论区域设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是存在的,但无论区域设置的方式在批处理文件拆分%DATE%(比方说,在3环境变量)?今天的日期是 2013年3月13日美国,但我的区域设置是 2013年3月13日 - < STRONG>分隔符改变以及订单以及

Is there a way to split the %date% in a batch file (say, in 3 environment variables), but regardless of Regional Settings? Today's date would be 3/13/2013 for US, but with my Regional Settings it is 13.3.2013 - the delimiter is changed and the order as well.

推荐答案

您可以做到这一点 (但不包括XP家庭WMIC):

You can do it using wmic (but WMIC isn't included with XP Home):

@ECHO OFF
:: Check WMIC is available
WMIC.EXE Alias /? >NUL 2>&1 || GOTO s_error

:: Use WMIC to retrieve date and time
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
   IF "%%~L"=="" goto s_done
      Set _yyyy=%%L
      Set _mm=00%%J
      Set _dd=00%%G
      Set _hour=00%%H
      SET _minute=00%%I
)
:s_done

:: Pad digits with leading zeros
      Set _mm=%_mm:~-2%
      Set _dd=%_dd:~-2%
      Set _hour=%_hour:~-2%
      Set _minute=%_minute:~-2%

:: Display the date/time in ISO 8601 format:
Set _isodate=%_yyyy%-%_mm%-%_dd% %_hour%:%_minute%
Echo %_isodate%

GOTO:EOF

:s_error
Echo GetDate.cmd
Echo Displays date and time independent of OS Locale, Language or date format.
Echo Requires Windows XP Professional, Vista or Windows 7
Echo.
Echo Returns 6 environment variables containing isodate,Year,Month,Day,hour and minute.

您可以通过解析date命令查找当前日期的格式是什么做的是必需的。

And you can do it by parsing the date command to lookup what the current date format is required to be.

第一个链接指向你可能需要编辑code在第二,Win7上,达到约短日期/长日期格式一些额外的皱纹。

The first link indicates you might need to edit the code in the second, on Win7, to handle a few extra wrinkles around short date/long date form.

这篇关于在批处理文件拆分%DATE%,无论区域设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆