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

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

问题描述

有没有办法将 %date% 拆分到批处理文件中(例如,在 3 个环境变量中),而不管区域设置如何?对于美国来说,今天的日期是 3/13/2013,但我的区域设置是 13.3.2013 - 分隔符已更改,顺序也已更改.

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.

推荐答案

你可以使用 wmic(但 WMIC 不包含在 XP Home 中):

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.

您可以通过解析日期命令来查找当前日期格式必须是.

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

第一个链接表明您可能需要在 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天全站免登陆