bat文件中的日期格式 [英] Date Formatting in a bat file

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

问题描述

我的批处理脚本的一部分涉及在批处理文件中创建时间戳. 我正在使用以下代码以yyyy-mm-dd格式获取日期 set mydate=!date:~10,4!-!date:~4,2!-!date:~7,2!

A part of my batch script involves creating a timestamp in the batch file. I am using the following code to get the date in yyyy-mm-dd format set mydate=!date:~10,4!-!date:~4,2!-!date:~7,2!

这在我的电脑上的设置为mm-dd-yy并且date命令返回时 The current date is: Mon 09/26/2016和我上面的命令将其转换为2016-09-26

This when the setting on my pc is mm-dd-yy and date command returns The current date is: Mon 09/26/2016 and my above command converts it into 2016-09-26

但是问题是,当我在具有dd-mm-yy格式的另一台计算机上运行脚本时,date命令返回以下命令:

but the problem is when i run my script on another machine which has a dd-mm-yy format where the date command returns this:

The current date is: 26-Sep-16和上面的命令给了我这个:-ep-16

The current date is: 26-Sep-16 and my above command gives me this: -ep-16

无论计算机上的日期格式设置如何,如何始终以所需的格式(yyy-mm-dd)获取日期?

How can i always get the date in my desired format (yyy-mm-dd) irrespective of the date format settings on the computer?

推荐答案

命令wmic os get localdatetime将给出类似

LocalDateTime
20160926085318.630000 + 120

LocalDateTime
20160926085318.630000+120

您可以将输出放在var中或直接将其拆分为多个变量.

you can than place the output in a var or directly split it to multiple variables.

对于单行输出,将开关/value添加到上面的命令中.输出将如下所示:

For a single line output add the switch /value to the command above. Output will then look like this:

LocalDateTime = 20160926085649.867000 + 120

LocalDateTime=20160926085649.867000+120

为显示功能概念背后的概念,我对此进行了弥补:

To show the concept behind the function idea, I made up this:

@echo off
setlocal EnableDelayedExpansion
REM change this to what you would do usually in your program:
for /l %%m in (1,1,5) do (
timeout /t 1
call:getNewTimestamp
echo !timestamp!
)
pause
goto:eof

:getNewTimestamp
for /f "delims== tokens=1*" %%g in ('wmic os get localdatetime /value') do (
if ".%%g"==".LocalDateTime" (
REM Change this to the usual way to get your timestamp:
set timestamp=%%h
)
)
Goto:eof

因此,每当需要当前时间戳时,都想要call :getNewTimeStamp.此功能会将!timestamp!设置为所需值.然后,您可以像平常一样在程序的主要部分中使用该值.
因此,我的示例存在一个循环,该循环经过5次,每次等待一秒钟,然后调用getNewTimestamp,然后回显!timestamp!的值.
术语function在这里可能会引起误解.它从同一脚本中更新脚本变量,最后将goto:eofcall <functionName>-命令一起使用,将导致根据功能脚本"更新变量.

So whenever you need the current timestamp, you want to call :getNewTimeStamp. This function will set !timestamp! to the desired value. You can then use the value as usual in your main part of the program.
So my example has a loop that it goes through 5 times, each time waiting a second, then calling getNewTimestamp and then echoing the value of !timestamp!.
The term function might be misleading here. It updates a script variable from within the same script, the goto:eof at the end in combination together with the call <functionName> - command, will result in updating the variable(s) accoring to the "functions script".

这篇关于bat文件中的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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