Computer Hope

Microsoft => Microsoft Windows => Windows XP => Topic started by: alain.roger on April 10, 2010, 12:52:20 AM

Title: command line to get time with 0
Post by: alain.roger on April 10, 2010, 12:52:20 AM
Hi,
i use a 24 hours time clock under my windows OS.
if i use a command line like the following ones:
Code: [Select]
set t=%time%
set tm=%t:~0,2%%t:~3,2%%t:~6,2%

and time is 09:52:38 in the morning, the %t% will be equal to " 9:52:38".
instead of that i would like to get "09:52:38", so the hours with a 0 in front of them.

how can i do that ?
thanks a lot,

A.
Title: Re: command line to get time with 0
Post by: Salmon Trout on April 10, 2010, 01:36:45 AM
1. Use string replacement to change all spaces in %tm% to zeroes. In fact there will only ever be one space, at the start, when the hour is 0 to 9.

Code: [Select]
@echo off
set t=%time%
set tm=%t:~0,2%%t:~3,2%%t:~6,2%
set tmzero=%tm: =0%
echo      t = "%t%"
echo     tm = "%tm%"
echo tmzero = "%tmzero%"

Output...

Code: [Select]
     t = " 8:32:14.07"
    tm = " 83214"
tmzero = "083214"

Alternatively...

2. Maybe you prefer to test if the first character (offset 0) of %tm% is a space, and if it is, make a new string composed of a zero character and the characters of %tm% from position 2 (offset 1) to the end


Code: [Select]
@echo off
set t=%time%
set tm=%t:~0,2%%t:~3,2%%t:~6,2%
if "%tm:~0,1%"==" " set tmzero=0%tm:~1%
echo      t = "%t%"
echo     tm = "%tm%"
echo tmzero = "%tmzero%"

Output...

Code: [Select]
     t = " 8:41:39.75"
    tm = " 84139"
tmzero = "084139"


Alain, since your name sounds like you are French or at least francophone. I would like to tell you that although I am rosbif de chez rosbif, I am a big fan of a French rock group called INDOCHINE. Have you heard of them?

Title: Re: command line to get time with 0
Post by: ghostdog74 on April 10, 2010, 01:46:20 AM
alternatively, download and use GNU date for windows (http://gnuwin32.sourceforge.net/packages/coreutils.htm)
Code: [Select]
C:\test>gnu_date.exe "+%H:%m:%S"
15:04:06
you can format the date/time however you want, plus a whole lot of other date/time features.
Code: [Select]
C:\test>gnu_date "+%H-%M-%S"
15-16-34
C:\test>gnu_date "+%Y-%m-%d-%H:%M:%S" -d "yesterday"
2010-04-09-15:57:23

Title: Re: command line to get time with 0
Post by: Salmon Trout on April 10, 2010, 01:50:00 AM
Here comes the "do it another way" brigade... in that vein, VBScript can be handy also...

Title: Re: command line to get time with 0
Post by: ghostdog74 on April 10, 2010, 01:54:54 AM
Here comes the "do it another way" brigade... in that vein, VBScript can be handy also...


it should be "do it in another hassle free way" :)
Title: Re: command line to get time with 0
Post by: alain.roger on April 10, 2010, 02:17:28 AM
thanks to everybody. i was not sure that adding a 0 before was allowed.
As i use this command line in a script i do not want to use any gnu_date. Moreover vbs script are not allowed in my company...due to security reason :-)

so i'm trying to have a clear and simple script in command line.

Salmon Trout :
yes i'm french and i know indochine... it's an old band mid-80's in light rock scene... somehow comparable to the cure.
but it was to slow and to soft for me :-)
Title: Re: command line to get time with 0
Post by: Salmon Trout on April 10, 2010, 02:39:24 AM
i know indochine... it's an old band mid-80's in light rock scene...

Ta gueule !  ;) Indochine est un groupe tellement moderne, vachement au courant chez les jeunes à mon avis... moi j'aime les albums Dancetaria (1999), Paradize (2001), Alice Et June (2005), La République Des Météors (2009), les collaborations avec Brian Molko, Mickey 3D, Dolly, etc...