Split

后端 未结 9 1371
感情败类
感情败类 2020-11-29 12:37

Is there a way to split the %date% in a batch file (say, in 3 environment variables), but regardless of Regional Settings? Today\'s date would be 3/13/201

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 12:57

    Below there is a pure Batch solution that just use DATE command and %DATE% variable, so it works on any Windows version. To make this program really universal, just add the name of the day and month parts shown by second line of DATE command in other languages, enclosed in slashes. For example, let's suppose that in German the day part is shown as TT, so you just need to modify this value: set DayNames=/dd/tt/ in order to also use this program in German language Windows (besides all the languages that use DD for the day part).

    @echo off
    setlocal EnableDelayedExpansion
    
    rem GetStdDate.bat: Get date in standard format independently of Regional Settings
    rem Antonio Perez Ayala
    
    rem Set Day and Month names shown by second line of DATE command, enclosed in slashes (add any missing language):
    set DayNames=/dd/
    set MonthNames=/mm/
    
    rem Get date NAMES from second line of date COMMAND, enclosed in parentheses
    for /F "skip=1 tokens=2 delims=()" %%n in ('date ^< NUL') do (
       rem Separate and store date names
       for /F "tokens=1-3 delims=/-.:" %%a in ("%%n") do (
          set one=%%a& set two=%%b& set three=%%c
          rem Get date VALUES from %date% VARIABLE
          for /F "tokens=1-3 delims=/-.:" %%x in ("%date%") do (
             rem Assign date values to date names
             set %%a=%%x& set %%b=%%y& set %%c=%%z
          )
       )
    )
    
    rem Identify locale date format and assemble StdDate=YYYY/MM/DD
    if "!DayNames:/%one%/=!" neq "%DayNames%" (
       rem Locale format is DD/MM/YYYY
       set StdDate=!%three%!/!%two%!/!%one%!
    ) else if "!MonthNames:/%one%/=!" neq "%MonthNames%" (
       rem Locale format is MM/DD/YYYY
       set StdDate=!%three%!/!%one%!/!%two%!
    ) else (
       rem Locale format is YYYY/MM/DD
       set StdDate=!%one%!/!%two%!/!%three%!
    )
    
    echo Standard date: %StdDate%
    

提交回复
热议问题