格式的日期和时间在Windows批处理脚本 [英] Format date and time in a Windows batch script

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

问题描述

在一个Windows(视窗  XP)的批处理脚本我需要格式化为在文件名等以后使用当前的日期和时间

这是一个类似堆栈溢出问题的 如何追加在批处理文件 的日期,但在和时间。

我有这个至今:

 回声%DATE%
回声%TIME%
设置datetimef =%日期:〜-4%_%日期:3,2〜_%日期%:〜0.2%__%的时间:〜0.2%_%时间:〜3,2%_%时间: 〜6.2%
回声%datetimef%

这给:

  28/07/2009
 8:35:31.01
2009_07_28__ 8_36_01

有没有一种方法,我可以允许在%TIME%单个数字小时,这样我就可以得到如下?

  2009_07_28__08_36_01


解决方案

我结束了与此脚本:

 设置小时=%的时间:〜0.2%
如果%时间:〜0.1%==集小时= 0%时:〜1,1%
回声小时=%小时%
设定分钟=%的时间:〜3,2%
如果%分:〜0.1%==集分= 0%以上:〜1,1%
回声分钟=%以上%
一套秒=%的时间:〜6,2%
如果%秒:〜0.1%==一套秒= 0%秒:〜1,1%
回声秒=%秒%设置年=%日期:〜-4%
回声年=%,比去年%

::在WIN2008R2例如我需要让你的设置一个月=%日期:3,2〜%'像下面:: 00,否则出现单月

 设置月=%日期:〜4.2%
如果%月:〜0.1%==设置月份= 0%,一个月:〜1,1%
回音月=%月%
设置天=%日期:0,2〜%
如果一天%:〜0.1%==设置天= 0%天:〜1,1%
回声天=%一天%设置datetimef =%年%%月%%一天%_%小时%%分钟%%秒%回声datetimef =%datetimef%

In a Windows (Windows XP) batch script I need to format the current date and time for later use in files names, etc.

It is similar to Stack Overflow question How to append a date in batch files, but with time in as well.

I have this so far:

echo %DATE%
echo %TIME%
set datetimef=%date:~-4%_%date:~3,2%_%date:~0,2%__%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %datetimef%

which gives:

28/07/2009
 8:35:31.01
2009_07_28__ 8_36_01

Is there a way I can allow for a single digit hour in %TIME%, so I can get the following?

2009_07_28__08_36_01

解决方案

I ended up with this script:

set hour=%time:~0,2%
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
echo hour=%hour%
set min=%time:~3,2%
if "%min:~0,1%" == " " set min=0%min:~1,1%
echo min=%min%
set secs=%time:~6,2%
if "%secs:~0,1%" == " " set secs=0%secs:~1,1%
echo secs=%secs%

set year=%date:~-4%
echo year=%year%

:: On WIN2008R2 e.g. I needed to make your 'set month=%date:~3,2%' like below ::otherwise 00 appears for MONTH

set month=%date:~4,2%
if "%month:~0,1%" == " " set month=0%month:~1,1%
echo month=%month%
set day=%date:~0,2%
if "%day:~0,1%" == " " set day=0%day:~1,1%
echo day=%day%

set datetimef=%year%%month%%day%_%hour%%min%%secs%

echo datetimef=%datetimef%

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

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