批处理脚本日期到变量中 [英] Batch script date into variable

查看:28
本文介绍了批处理脚本日期到变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (
set Day=%%k
set Month=%%j
set Year=%%l
set DATE=%%k/%%j/%%l)

我试图在批处理脚本中将日期放入上述变量中​​,但目前日期显示为

I am try to get the date into the above variables in a batch script, but currently the date comes out as

2011/04/

关于如何解决这个问题有什么建议吗?

Any suggestions on how to fix this?

推荐答案

您没有得到预期的结果,因为 %DATE% 使用短日期格式"的 Windows 设置返回当前日期".此设置是完全(无限)可定制的.

You don't get what you expected because %DATE% returns the current date using the windows settings for the "short date format". This setting is fully (endlessly) customizable.

一个用户可以将其系统配置为将短日期显示为 Fri040811;而另一个用户(即使在同一系统中)可能会选择 08/04/2011.这对 BAT 程序员来说简直就是一场噩梦.

One user may configure its system to show the short date as Fri040811; while another user (even in the same system) may choose 08/04/2011. It's a complete nightmare for a BAT programmer.

一种可能的解决方案是改用 WMIC.WMIC 是 WMI 的 WMI 命令行界面.WMI Windows Management Instrumentationhttp://en.wikipedia.org/wiki/Windows_Management_Instrumentation

One possible solution is to use WMIC, instead. WMIC is the WMI command line interface to WMI. WMI Windows Management Instrumentation is the http://en.wikipedia.org/wiki/Windows_Management_Instrumentation

WMIC Path Win32_LocalTime Get Day,Hour,Minute,Month,Second,Year /Format:table

以方便的方式返回日期,直接用FOR解析它.

returns the date in a convenient way to directly parse it with a FOR.

完成解析并将各个部分放在一起

Completing the parse and putting the pieces together

 FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
    SET /A TODAY=%%F*10000+%%D*100+%%A
 )

这篇关于批处理脚本日期到变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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