• 批处理bat标准化获取当前系统日期的几种方法,补0


    首先有两个推荐的方案。

    一:

    for /f "tokens=2 delims==" %%a in ('wmic path win32_operatingsystem get LocalDateTime /value') do (set t=%%a)
    set Today=%t:~0,4%%t:~4,2%%t:~6,2%%t:~8,2%%t:~10,2%
    echo %Today%

    二:

    if %time:~0,2% LEQ 9 (echo %date:~2,2%%date:~5,2%%date:~8,2%0%time:~1,1%%time:~3,2%) else (echo %date:~2,2%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%) 

     若要继续研究请阅读下面文章:

     使用0替换空格

    有时候我们需要获取当前系统的时间,如果小时小于10的话,获取小时会出现空格的情况:
    
    c:>time
    当前时间:  8:52:38.05
    c:>echo %time:~0,2%
     8    ------这个8前面是有空格的
    
    c:>
    如何去掉空格让时间正确的显示呢?其实只需要将空格替换成0就可以了。
    
    c:>set h=%time:~0,2%
    
    c:>echo %h: =0%  -----这里的‘=’就是‘替换’的含意
    08
    
    c:>
    你也可以将这里的空格替换成其它值:
    c:>echo %h: =aaaaa%
    aaaaa8
    那现在获取当前系统的日期和时间就可以这么写了:
    
    c:>set h=%time:~0,2%
    
    c:>set h=%h: =0%
    
    c:>echo %date:~0,4%%date:~5,2%%date:~8,2%%h%%time:~3,2%%time:~6,2%
    20150324090254
     
    原:http://blog.csdn.net/chinadm123/article/details/44588891

    多格式兼容

    操作系统不同,日期格式也可能不同:
    星期二 2008-07-29
    2008-07-29 星期二
    07/29/2008 Tue
    Tue 07/29/2008
     
    再考虑到中、英之外的其它语言的操作系统,日期格式的种类实在太多了。
    要想标准化地获取当前系统日期2008-07-28,直接截取%date%变量的方法是不行的。
    【方案一】BAT + REG
    @echo off
    rem 无法保证在中、英之外的其它语言的操作系统上得到正确结果
    for /f "delims=" %%a in ('reg query "HKEY_CURRENT_USER/Control Panel/International" /v sShortDate') do (
      set "RegDateOld=%%a"
    )
    set RegDateOld=%RegDateOld:~-8%
    reg add "HKEY_CURRENT_USER/Control Panel/International" /v sShortDate /t REG_SZ /d yyyy-M-d /f>nul
    set Today=%date: =%
    reg add "HKEY_CURRENT_USER/Control Panel/International" /v sShortDate /t REG_SZ /d %RegDateOld% /f>nul
    set "Week=Mon Tue Wed Thu Fri Sat Sun 星期一 星期二 星期三 星期四 星期五 星期六 星期日"
    for %%a in (%Week%) do (
      call set "Today=%%Today:%%a=%%"
    )
    echo,%Today%
    pause
    【方案二】BAT + REG
    @echo off
    for /f "delims=" %%a in ('reg query "HKEY_CURRENT_USER/Control Panel/International" /v sShortDate') do (
      set "RegDateOld=%%a"
    )
    set RegDateOld=%RegDateOld:~-8%
    reg add "HKEY_CURRENT_USER/Control Panel/International" /v sShortDate /t REG_SZ /d yyyy-M-d /f>nul
    set Today=%date: =%
    reg add "HKEY_CURRENT_USER/Control Panel/International" /v sShortDate /t REG_SZ /d %RegDateOld% /f>nul
    if "%Today:~0,1%" gtr "9" (
      set Today=%Today:~-10%
    ) else (
      set Today=%Today:~0,10%
    )
    echo,%Today%
    pause
    【方案三】BAT + REG
    @echo off
    for /f "delims=" %%a in ('reg query "HKEY_CURRENT_USER/Control Panel/International" /v sShortDate') do (
      set "RegDateOld=%%a"
    )
    set RegDateOld=%RegDateOld:~-8%
    reg add "HKEY_CURRENT_USER/Control Panel/International" /v sShortDate /t REG_SZ /d yyyy-M-d /f>nul
    type nul>"%temp%/MyFile.tmp"
    for /f %%a in ('dir "%temp%/MyFile.tmp" ^| findstr /i "MyFile.tmp"') do (
      set Today=%%a
    )
    reg add "HKEY_CURRENT_USER/Control Panel/International" /v sShortDate /t REG_SZ /d %RegDateOld% /f>nul
    echo,%Today%
    pause
    【方案四】BAT + WMIC
    @echo off
    for /f "tokens=2 delims==" %%a in ('wmic path win32_operatingsystem get LocalDateTime /value') do (
      set t=%%a
    )
    set Today=%t:~0,4%-%t:~4,2%-%t:~6,2%
    echo,%Today%
    pause
    【方案五】BAT + VBS
    @echo off
    >"%temp%/DateCalculate.vbs" echo dt=date()
    >>"%temp%/DateCalculate.vbs" echo s=right(year(dt),4) ^& "-" ^& right("0" ^& month(dt),2) ^& "-" ^& right("0" ^& day(dt),2)
    >>"%temp%/DateCalculate.vbs" echo wscript.echo s
    for /f %%a in ('cscript /nologo "%temp%/DateCalculate.vbs"') do set (
      Today=%%a
    )
    echo,%Today%
    pause
    【方案六】BAT + REGEDIT
    @echo off
    rem 需要保证注册表编辑器没有处于锁定状态
    regedit /e "%temp%/bak.reg" "HKEY_CURRENT_USER/Control Panel/International"
    >"%temp%/new.reg" echo REGEDIT4
    >>"%temp%/new.reg" echo,
    >>"%temp%/new.reg" echo [HKEY_CURRENT_USER/Control Panel/International]
    >>"%temp%/new.reg" echo "sShortDate"="yyyy-MM-dd"
    regedit /s "%temp%/new.reg"
    set Today=%date: =%
    regedit /s "%temp%/bak.reg"
    if "%Today:~0,1%" gtr "9" (
      set Today=%Today:~-10%
    ) else (
      set Today=%Today:~0,10%
    )
    echo,%Today%
    pause 
    【方案七】BAT + DEBUG
     @echo off
    for /f "tokens=6,8 delims== " %%a in ('^(echo a100^&echo mov ah^,2a^&echo int 21^&echo.^&echo p 2^&echo q^)^|debug^|find "CX"') do (
      set /a y=0x%%a
      set md=%%b
    )
    set /a m=0x%md:~,2%
    set /a d=0x%md:~-2%
    set m=0%m%
    set d=0%d%
    set Today=%y%-%m:~-2%-%d:~-2%
    echo,%Today%
    pause
    
    原:http://www.jb51.net/article/52749.htm
    View Code
  • 相关阅读:
    java基础之java今生前世
    java线程的方便调用方式
    await和async更多的理解
    asp.net mvc webapi 实用的接口加密方法
    微信小程序开发心得
    为IEnumerable<T>添加RemoveAll<IEnumerable<T>>扩展方法--高性能篇
    微软常用的组件设计思想-‘工厂的工厂类’
    尝试asp.net mvc 基于controller action 方式权限控制方案可行性
    cookie 和session 详解
    sql注入详解
  • 原文地址:https://www.cnblogs.com/daysme/p/6571926.html
Copyright © 2020-2023  润新知