Batch BAT normalization several methods for obtaining the current system date _dos/bat

Source: Internet
Author: User

The date format may vary, depending on the operating system:

Tuesday 2008-07-29
2008-07-29 Tuesday
07/29/2008 Tue
Tue 07/29/2008

Taking into account the operating system in languages other than Chinese and English, there are too many types of date formats.
To standardize the current system date 2008-07-28, the direct interception of%date% variables is not possible.

"Scheme one" BAT + REG

@echo
off REM does not guarantee correct results on operating systems in languages other than China
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 "W Eek=mon Tue Wed Thu Fri Sat Sun Monday Tuesday Wednesday Thursday Friday Saturday Sunday "for
%%a in (%week%) does (call
  set today=%%today:%% a=%% "
)
echo,%today%
Pause

"Scheme two" 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/contr OL panel/international "/V sshortdate/t reg_sz/d%regdateold%/f>nul
If"%today:~0,1% "GTR" 9 "(
  set Today=%t oday:~-10%
) Else (
  set today=%today:~0,10%
)
echo,%today%
Pause
   

"Scheme three" 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
   

"Scheme four" BAT + WMIC

@echo off
for/f "tokens=2 delims=="%%a in (' WMIC path Win32_OperatingSystem get Localdatetime/value ') do (
  SE T t=%%a
)
set today=%t:~0,4%-%t:~4,2%-%t:~6,2%
echo,%today%
Pause

"Scheme five" 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)
>> "%te" Mp%/datecalculate.vbs "Echo WScript.Echo S
for/f%%a in (' Cscript/nologo"%temp%/datecalculate.vbs "') do set (
  T Oday=%%a
)
echo,%today%
Pause

"Scheme six" BAT + REGEDIT

@echo off
REM need to ensure Registry Editor is not locked
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 

"Scheme seven" BAT + DEBUG

 @echo off
for/f "tokens=6,8 delims=="%%a in (' ^ "(echo a100^&echo mov ah^,2a^&echo int 21^&echo.^&ec Ho P 2^&echo q^) ^|debug^|find "CX") do (
  set/a y=0x%%a
  set md=%%b
)
set/a m=0x%md:~,2%
SE t/a d=0x%md:~-2%
set m=0%m%
set d=0%d%
set today=%y%-%m:~-2%-%d:~-2%
echo,%today%
Pause

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.