date not showing

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jap
Posts: 6
Joined: 27 May 2017 08:42

date not showing

#1 Post by jap » 11 Oct 2019 13:00

Hi,

any idea why the date does not show up in one pc? other pc when i run this batch included in my script it runs fine

@echo off

for /F "tokens=2" %%i in ('date /t') do set mydate=%%i
set mytime=%time%
echo Current time is %mydate%:%mytime%

turned off uac
pc's running on windows 10

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

Re: date not showing

#2 Post by Compo » 11 Oct 2019 14:44

The returned date from date /t is not consistent across PC's or users. I'm assuming that you were previously seeing ddd and trying to ignore it by selecting the second token. You can still use the same method, if you like, by just checking for an empty value for the second token:

Code: Select all

For /F "Tokens=1*" %%A In ('Date /T') Do If "%%B"=="" Set "mydate=%%A" Else Set "mydate=%%B"

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Re: date not showing

#3 Post by Hackoo » 11 Oct 2019 18:25

I recommend you using the date independently of the region day/month order, you can use "WMIC os GET LocalDateTime" as a source, since it's in ISO order:

Code: Select all

@echo off
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldt=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2% %ldt:~8,2%:%ldt:~10,2%:%ldt:~12,6%
echo Local date is [%ldt%]
pause
Refer to this Source https://stackoverflow.com/questions/203 ... ve#tab-top

jap
Posts: 6
Joined: 27 May 2017 08:42

Re: date not showing

#4 Post by jap » 14 Oct 2019 10:03

Hackoo wrote:
11 Oct 2019 18:25
I recommend you using the date independently of the region day/month order, you can use "WMIC os GET LocalDateTime" as a source, since it's in ISO order:

Code: Select all

@echo off
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldt=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2% %ldt:~8,2%:%ldt:~10,2%:%ldt:~12,6%
echo Local date is [%ldt%]
pause
Refer to this Source https://stackoverflow.com/questions/203 ... ve#tab-top
this was the solution - WMIC os GET LocalDateTime
thanks Hackoo!

Post Reply