Warning: count(): Parameter must be an array or an object that implements Countable in /var/www/vhosts/shan.info/httpdocs/components/com_k2/models/item.php on line 1198

Warning: count(): Parameter must be an array or an object that implements Countable in /var/www/vhosts/shan.info/httpdocs/components/com_k2/models/item.php on line 763

在不同日期格式下批处理输出自定义格式的日期和时间

在Windows系统中有不同的语言版本和时区设置。有时,由于业务的需求,我们需要生成“年月日”格式的日期格式目录,然后在该目录中生成时间格式的文件,用来记录系统当前时间的日志信息。

系统日期有yy-mm-dd,mm-dd-yy以及dd-mm-yy三种格式,这样导致了使用的不方便。下面介绍几种实现方法:

  1. 有 人通过预先修改windows注册表将日期改成yy-mm-dd格式然后通过发出命令:c:\> echo %date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%这种该注册表的方法显然不是很可取。
  2. 还有人通过vbs来实现,制作成批处理文件内容如下:@echo off
    echo wscript.echo year(date) ^& right(”0″ ^& month(date),2) ^& right(”0″ ^& day(date),2) ^& right(”0″ ^& hour(time),2) ^& right(”0″ ^& minute(time),2)>%tmp%\temp1.vbs
    cscript /nologo %tmp%\temp1.vbs
    del %tmp%\temp1.vbs 

    这种使用script的方式也不是很好。

  3. 我认为最好的方式是,通过批处理中的for语句进行系统日期格式的判断,然后进行处理。批处理文件内容如下:

@ECHO OFF

ECHO. |DATE > temp1
FOR /F “eol=T tokens=5 delims=() ” %%i in (temp1) do set date_format=%%i
FOR /F “eol=E tokens=5,6,7,8 delims=/- ” %%a in (temp1) do (
    IF /I %date_format% EQU mm-dd-yy ( set month=%%a
                                       set day=%%b
                                       set year=%%c
                                       set week=%%d )
    IF /I %date_format% EQU dd-mm-yy ( set day=%%a
                                       set month=%%b
                                       set year=%%c
                                       set week=%%d )
    IF /I %date_format% EQU yy-mm-dd ( set year=%%a
                                       set month=%%b
                                       set day=%%c
                                       set week=%%d )      )

ECHO. |TIME > temp2
FOR /F “eol=E tokens=5-8 delims=:. ” %%A in (temp2) do ( set hour=%%A
                                                         set minute=%%B
                                                         set second=%%C
                                                         set hundredth=%%D )
                                                        
REM TIME format 08:10:00 PM, it means 20:10:00
REM IF %meridiem% EQU PM SET /A %%hour%%=%%hour%%+12

SET current_date=%year%%month%%day%
SET current_time=%hour%%minute%%second%%hundredth%

IF EXIST temp1 DEL /Q temp1
IF EXIST temp2 DEL /Q temp2

ECHO Current date = %current_date%
ECHO Current time = %current_time%

MKDIR %current_date%
ECHO. > %current_date%\%current_time%.log

Last modified onSunday, 14 October 2012 04:07

Warning: count(): Parameter must be an array or an object that implements Countable in /var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php on line 169

Notice: Only variables should be assigned by reference in /var/www/vhosts/shan.info/httpdocs/templates/gk_publisher/html/com_k2/templates/default/item.php on line 478
back to top