convert date in yyyymmdd

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sal21
Posts: 22
Joined: 12 Jul 2016 12:58
Location: Italy

convert date in yyyymmdd

#1 Post by sal21 » 13 Jul 2016 01:00

Code: Select all


:: get the file's date as dd/mm/yyyy
for /f "tokens=1" %%a in ('dir "%folder%\%file%" ^| find /i "%file%"') do set filedate=%%a

:: change the date to yyyymmdd
set date_created=%filedate:~10,4!!filedate:~7,2!!filedate:~4,2!



the set date_created not return date in yyyymmdd format!
Why?

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: convert date in yyyymmdd

#2 Post by Squashman » 13 Jul 2016 05:47

Date settings are region dependent. We need to see the output of your DIR command to see what format it is in.

Regardless of that, you should really try using the variable modifiers to just get the date.

Code: Select all

In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line

In the above examples %I and PATH can be replaced by other valid
values.  The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: convert date in yyyymmdd

#3 Post by foxidrive » 14 Jul 2016 08:37

Here's some code to get a specific date format in any region.


Code: Select all

rem The four lines below will give you reliable YY DD MM YYYY HH Min Sec MS variables in XP Pro and higher.
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%" & set "MS=%dt:~15,3%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" & set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%-%MS%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause

Code: Select all

for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "date-time=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%dt:~8,2%-%dt:~10,2%-%dt:~12,2%"
echo date-time: "%date-time%"
pause

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: convert date in yyyymmdd

#4 Post by SIMMS7400 » 16 Jul 2016 19:37

I use this method in all my scripts:

Code: Select all

@ECHO OFF

FOR /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set timestamp=%%a%%b)
FOR /f "tokens=* delims= " %%c in ("%timestamp%") do (set timestamp=%%c)

ECHO %date:~-4,4%%date:~-10,2%%date:~-7,2%_%timestamp%_%~n0 >>C:\DOS_TIPS_FORUMS.txt


It outputs this:

20160716_2136_DOS_TIPS_FORUMS


Hope this helps!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: convert date in yyyymmdd

#5 Post by foxidrive » 17 Jul 2016 11:26

SIMMS7400 wrote:I use this method in all my scripts:

FOR /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set timestamp=%%a%%b)
ECHO %date:~-4,4%%date:~-10,2%%date:~-7,2%_%timestamp%_%~n0


That works fine, but in other parts of the world it will fail. That's the problem that MS never fixed. Even on a computer in the house next door to you a person can alter the regional settings and your script will fail for them.

I think the programmer at Microsoft was high on magic mushrooms when he wrote the code for date and time. :D

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: convert date in yyyymmdd

#6 Post by Squashman » 17 Jul 2016 14:16

The user is asking for the date of a file in a specific format. Not the current date in a specific format.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: convert date in yyyymmdd

#7 Post by Compo » 18 Jul 2016 03:11

Once again I'm drawn to powershell…

Code: Select all

:: get the file's date as yyyyMMdd
For /F UseBackQ %%a In (
   `Powershell "(dir '%folder%\%file%').LastWriteTime.ToString('yyyyMMdd')"`
) Do Set MyVar=%%a
You can change LastWrite to Creation if it suits your needs better!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: convert date in yyyymmdd

#8 Post by foxidrive » 18 Jul 2016 04:37

Squashman wrote:The user is asking for the date of a file in a specific format. Not the current date in a specific format.

I am president of the local pedantic club. LOL

He's asking why the date format was wrong and didn't mention a file at all. :) But yes, he didn't mention the current date.

I'll have to resign my position at the club..

I have to admit that I don't pay attention to code that has no explanation. These days there is no guarantee that the code even represents what the user has on their computer.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: convert date in yyyymmdd

#9 Post by thefeduke » 19 Jul 2016 15:56

The original post had two lines of batch code, so here my two lines:

Code: Select all

:: get the file's date as dd/mm/yyyy
for %%D in ("%folder%\%file%") do Set "filedate=%%~tD"
           
:: change the date to yyyymmdd
set date_created=%filedate:~6,4%%filedate:~3,2%%filedate:~,2%

Please note that the date is that of last update and not the creation date and the regional date format is a default that seems to work for you. I have added some code to demonstrate the difference and how to extract the creation date if that is what you really wanted:

Code: Select all

@echo off &setlocal
::Posted: Wed Jul 13, 2016 3:00 am by sal21   
::http://www.dostips.com/forum/viewtopic.php?p=47743#p47743
:: Post subject: convert date in yyyymmdd
::modified: Tue Jul 19, 2016 by thefeduke

Set "folder=%Userprofile%\scripts\testdata"
Set "file=FileDate.txt"

:: get the file's date as dd/mm/yyyy
for %%D in ("%folder%\%file%") do Set "filedate=%%~tD"
:: change the date to yyyymmdd
set date_created=%filedate:~6,4%%filedate:~,2%%filedate:~3,2%
Echo.File Default date is : %date_created%

:: get the file's date as dd/mm/yyyy using default write date in dir
for /f "tokens=1-5" %%A in ('dir "%folder%\%file%" /A-d') do (
  if /I "%%E" EQU "%file%" Set "filedate=%%~A"
)         
:: change the date to yyyymmdd
set date_created=%filedate:~6,4%%filedate:~,2%%filedate:~3,2%
Echo.File Default date is : %date_created%

:: get the file's date as dd/mm/yyyy using create date in dir
for /f "tokens=1-5" %%A in ('dir "%folder%\%file%" /A-d /Tc') do (
  if /I "%%E" EQU "%file%" Set "filedate=%%~A"

:: change the date to yyyymmdd
set date_created=%filedate:~6,4%%filedate:~,2%%filedate:~3,2%
Echo.File Creation date is: %date_created%

Exit /b

John A.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: convert date in yyyymmdd

#10 Post by foxidrive » 20 Jul 2016 12:47

thefeduke wrote:

Code: Select all

:: get the file's date as dd/mm/yyyy
for %%D in ("%folder%\%file%") do Set "filedate=%%~tD"

John A.


The %%~tD format depends on the region it is being used in John so it's unreliable for setting a specific date format. Even user changes in the control panel will alter this.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: convert date in yyyymmdd

#11 Post by thefeduke » 22 Jul 2016 16:51

foxidrive wrote:The %%~tD format depends on the region it is being used in John so it's unreliable for setting a specific date format. Even user changes in the control panel will alter this.
So I followed foxi's lead and went to the control panel to see what I could do:

Code: Select all

@Echo off&SetLocal
::Posted: Wed Jul 13, 2016 3:00 am by sal21   
::http://www.dostips.com/forum/viewtopic.php?p=47743#p47743
:: Post subject: convert date in yyyymmdd
::modified: Tue Jul 22, 2016 by thefeduke

Set "folder=%Userprofile%\scripts\testdata"
Set "file=FileDate.txt"

:: Get the date format from registry settings
For /F "usebackq tokens=3 delims= " %%F in (`reg query "HKCU\Control Panel\International"  /V sShortDate`) do Set "sDateFormat=%%F"
Rem.Echo.sDate format: %sDateFormat%

:: Create date format table for US English
>"%temp%\%~n0_sDate.txt" (
Echo.dd-MMM-yy  Echo 2-digit Year tested
Echo.yy/MM/dd   Echo 2-digit Year tested
Echo.yyyy-MM-dd 0 5 8             tested
Echo.MM/dd/yy   Echo 2-digit Year tested
Echo.MM/dd/yyyy 6 0 3             tested
Echo.M/d/yyyy   6 0 3             tested
Echo.M/d/yy     Echo 2-digit Year tested
)

:: Set offsets for date format
Set "OffY="&Set "OffM="&Set "OffD="
For /F "usebackq tokens=1-4* delims= " %%D in (`type "%temp%\%~n0_sDate.txt"`) do (
    IF "%%~D" EQU "%sDateFormat%" (
        If /I "%%E" EQU "echo" (
            %%E %%F %%G %%H for setting: %%D
        ) Else Set "OffY=%%E"&Set "OffM=%%F"&Set "OffD=%%G"
    )
)
If NOT Defined Offy (Echo.Unexpected date format.&Exit /b 1 )

:: get file's date in regional format using create date in DIR command
for /f "tokens=1-5" %%A in ('dir "%folder%\%file%" /A-d /Tc') do (
  if /I "%%E" EQU "%file%" Set "filedate=%%~A"
)
           
:: change the date to yyyymmdd using custom offsets for date format
setlocal enableDelayedExpansion
set date_created=!filedate:~%OffY%,4!!filedate:~%OffM%,2!!filedate:~%OffD%,2!
Echo.File Creation date is: %date_created%

Exit /B
I did not attempt to work with the two digit year formats, but it is much better than before. I only tested on Windows 7.
Compo wrote:Once again I'm drawn to powershell…
Abandon ship! I found Compo's solution vastly superior in every way that I could see.

At least this pure batch improvement works in some cases and indicates that it cannot when appropriate.

John A.

Post Reply