Display current date and time

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Display current date and time

#1 Post by drgt » 30 Oct 2016 07:05

Hi
How to echo current date and time on the same line?

Thanks

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Display current date and time

#2 Post by penpen » 30 Oct 2016 08:04

This should help you:

Code: Select all

echo %date% %time%


penpen

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Display current date and time

#3 Post by drgt » 30 Oct 2016 10:02

How about the day too like
Sun 2016-10-30 17:55:42.35 or
Sunday 2016-10-30 17:55:42.35

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Display current date and time

#4 Post by ShadowThief » 30 Oct 2016 11:09

Oh geez, you've got one of those locales where %date% doesn't show the day of the week for some reason.

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Display current date and time

#5 Post by drgt » 30 Oct 2016 11:12

yep, I am reading on this right now...

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

Re: Display current date and time

#6 Post by aGerman » 30 Oct 2016 12:54

WMIC should always be available nowadays.

Code: Select all

for %%i in ("0=Sun" "1=Mon" "2=Tue" "3=Wed" "4=Thu" "5=Fri" "6=Sat") do set "wd%%~i"
for /f %%i in ('WMIC OS GET LocalDateTime /value') do for /f "tokens=2 delims==" %%j in ("%%i") do set "dt=%%j"
for /f %%i in ('WMIC PATH Win32_LocalTime GET DayOfWeek /value') do for /f "tokens=2 delims==" %%j in ("%%i") do (
  call set "dt=%%wd%%j%% %dt:~,4%-%dt:~4,2%-%dt:~6,2% %dt:~8,2%:%dt:~10,2%:%dt:~12,5%")

echo %dt%


Steffen

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Display current date and time

#7 Post by drgt » 30 Oct 2016 13:42

Great job Steffen!
Thanks!

Since I am clueless on the FOR usage in batch and of course "regurgitating" is "give a man a fish and you feed him for a day", should you have the time and want to, please explain this code to me in order to "teach a man to fish and you feed him for a lifetime" !

:D
Thanks again

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

Re: Display current date and time

#8 Post by aGerman » 30 Oct 2016 13:59

I see your point. Did you ever read a tutorial about FOR loops in batch? If not please do this as soon as you have time. Understanding how loops work is the key for writing good code in any language.

Steffen

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Display current date and time

#9 Post by drgt » 30 Oct 2016 14:01

Any links you propose would be appreciated.

Thanks again


drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Display current date and time

#11 Post by drgt » 30 Oct 2016 14:11

Great!
Take care and thanks again.

Post Reply