Time is set incorrectly after midnight

后端 未结 4 719
逝去的感伤
逝去的感伤 2020-11-27 23:11

i am using the following to set the date/time to something more readable:

set day=%date:~4,2%
set mth=%date:~7,2%
set yr=%date:~10,4%
set hur=%time:~0,2%
set         


        
4条回答
  •  悲哀的现实
    2020-11-27 23:47

    The problem is that you're using the %DATE% and %TIME% variable values, which contain strings which are not consistent across locales, PC's, or users.

    The best advice is to retrieve the information using an alternative method, for example:

    @For /F "Tokens=1-5Delims=/: " %%G In (
        '""%__AppDir__%Robocopy.exe" \: . /NJH /L|"%__AppDir__%find.exe" " 123""'
    )Do @Set "bdate=[%%I-%%H-%%G]-[%%J-%%K]"
    

    The method involves generating an error message from the built-in robocopy utility. We do that by asking it to copy from a target directory named : in the root of the current drive, \. An error is returned because Windows does not allow directory names containing the character :. That error message, regardless of locale, PC, or user, always outputs a line starting with the date and time strings in a known format, yyyy/MM/dd hh:mm:ss.

    Examples:

    2020/02/08 01:25:04 ERROR 123 (0x0000007B) Accessing Source Directory C:\:\
    The filename, directory name, or volume label syntax is incorrect.
    

    2020/02/08 01:25:04 ОШИБКА 123 (0x0000007B) Доступ к исходной папке C:\:\
    ‘Ё*в*ЄбЁзҐбЄ*п ®иЁЎЄ* ў Ё¬Ґ*Ё д*©«*, Ё¬Ґ*Ё Ї*ЇЄЁ Ё«Ё ¬ҐвЄҐ ⮬*.
    

    2020/02/08 01:25:04 ERREUR 123 (0x0000007B) Copie du fichier C:\:\
    La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte.
    

    2020/02/08 01:25:04 FEHLER 123 (0x0000007B) Zugriff auf Zielverzeichnis C:\:\
    Die Syntax für den Dateinamen, Verzeichnisnamen oder die Datenträgerbezeichnungist falsch.
    

    We run our Robocopy command inside a for-loop in order that we can capture the required strings according to their known line position, format, and separator characters. We also need to account for the fact that the output consists of more than one line. To select the line, I have used the built-in find utility, and chosen to match only lines containing the string  123 as it cannot appear elsewhere in the output. Next I split the line up using the known separator characters as delimiters and choose how many delimiter separated token components to return. I choose a forward slash, /, which delimits the individual date components, a colon. :, which delimits the individual time components, and a space,  , which separates date and time from the rest of the line and each other. For your user case you only wanted five token components:

        ↓  ↓  ↓  ↓  ↓    delims
    2020/02/08 01:25:04
    ↑    ↑  ↑  ↑  ↑
     1    2  3  4  5     tokens
     %G  %H %I %J %K

    The final thing I do is to put each of the components together in the required order and format, saving it as a string value within a variable named bdate. You should be able to use that value elsewhere in the script, as %bdate%, or !bdate! if delayed expansion is enabled.

    To get more information of how to use a for-loop, please open a Command Prompt window and enter for /?.

提交回复
热议问题