Transform date time

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Transform date time

#1 Post by darioit » 21 Sep 2010 08:18

Hello everybody,

how can I transform this "21/09/2010 16.04.32"

to this format "2010-09-21 16:04:32.00"

Regards
Dario

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Transform date time

#2 Post by aGerman » 21 Sep 2010 08:28

Use a FOR loop

Code: Select all

@echo off &setlocal
set "oldDatetime=21/09/2010 16.04.32"
for /f "tokens=1-6 delims=/. " %%a in ("%oldDatetime%") do set "newDatetime=%%c-%%b-%%a %%d:%%e:%%f.00"
echo %newDatetime%
pause


Regards
aGerman

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Transform date time

#3 Post by darioit » 22 Sep 2010 02:13

Thanks it works,

but if the hour is below 10 I loose 0 below

8:52:28.00 instead 08:52:28.00

the script to take date/time of file is this

Code: Select all

>"%temp%\tmp.vbs" ECHO WScript.Echo WScript.CreateObject("Scripting.FileSystemObject").GetFile(WScript.Arguments(0)).DateLastModified
FOR /f "delims=" %%a IN ('cscript //nologo "%temp%\tmp.vbs" "%2"') DO SET "DateTime=%%a"


Regards
Dario

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Transform date time

#4 Post by orange_batch » 22 Sep 2010 02:51

Append leading characters:

Code: Select all

set hour=8
set hour=0%hour%
set hour=%hour:~-2%
set minutes=23
echo:%hour%:%minutes%

08:23

Code: Select all

set hour=11
set hour=0%hour%
set hour=%hour:~-2%
set minutes=23
echo:%hour%:%minutes%

11:23

or, should be faster:

Code: Select all

setlocal enabledelayedexpansion
set hour=8
if %hour% LSS 10 set hour=0%hour%&set hour=!hour:~-2!
set minutes=23
echo:%hour%:%minutes%

08:23

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Transform date time

#5 Post by aGerman » 22 Sep 2010 16:13

Yeah. To make it in the same for loop you could use

Code: Select all

@echo off &setlocal
set "oldDatetime=21/09/2010 6.04.32"
for /f "tokens=1-6 delims=/. " %%a in ("%oldDatetime%") do (set "H=0%%d" &call set "newDatetime=%%c-%%b-%%a %%H:~-2%%:%%e:%%f.00")
echo %newDatetime%
pause


Regards
aGerman

Post Reply