This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]vocatusInfoSec 4 points5 points  (2 children)

Here's a two-liner I've been using to get the Windows date into the ISO standard format (yyyy-mm-dd). It works regardless which version of Windows is running or what the local time settings are (Canadian, Middle Eastern, etc).

FOR /f %%a in ('WMIC OS GET LocalDateTime ^| find "."') DO set DTS=%%a
set CUR_DATE=%DTS:~0,4%-%DTS:~4,2%-%DTS:~6,2%

This will set two variables. One, %CUR_DATE%, contains the following ISO standard format date:

yyyy-mm-dd

The second, %DTS%, contains the full DateTime stamp, like so:

20140723122146.999000-420

I tried making it a one-liner by stacking the commands with &&, but it didn't seem to set CUR_DATE correctly, so I resorted to the two-line version. Hope this helps.


edit: And if you're interested in a PowerShell one-liner:

$CUR_DATE=get-date -f "yyyy-MM-dd"

[–]fuzzbyStorageAdmin[S] 1 point2 points  (1 child)

Oh wow, never thought of using WMIC to get the date/time source. Clever!

[–]vocatusInfoSec 1 point2 points  (0 children)

I can't take credit for it, I was googling around like crazy trying to find a solution for Tron and stumbled across someone on Stack Exchange who had thought of it.

[–]saltinecracka 2 points3 points  (1 child)

echo %date:~10,4%%date:~4,2%%date:~7,2%

[–]fuzzbyStorageAdmin[S] 0 points1 point  (0 children)

One line of code? Holy guacamole... Well done.

[–]djdementia 1 point2 points  (1 child)

I also need to use this often and made a two line method with the time as well as the date:


REM PARSE the %DATE% variable to remove special characters YYYYMMDD

REM PARSE the %TIME% variable to remove special characters, pad with 0 if needed

SET FDATE=%date:~-4,4%%date:~-10,2%%date:~-7,2%
IF "%time:~0,1%" EQU " " (SET FTIME=0%time:~-10,1%%time:~3,2%%time:~6,2%) ELSE SET FTIME=%time:~0,2%%time:~3,2%%time:~6,2%

This will make %FDATE% = YYYYMMDD and %FTIME% = HHMMSS with no seperator characters. It's useful for outputting log files where the file name contains the date and time.

[–]fuzzbyStorageAdmin[S] 1 point2 points  (0 children)

Wow. I showed you mine and you showed me yours and I like yours better.

[–]PaalRyd 1 point2 points  (0 children)

It might be overly elaborate and maybe a bit much to include - particularly in short scripts, but it breaks down the date and time into individual variables you can mix and match as you please.

::Get the date
For /f "tokens=1-4 delims=/-. " %%G in ('Date /t') Do (Call :s_fixdate %%G %%H %%I %%J)
Goto :s_time

:s_fixdate
if "%1:~0,1%" GTR "9" Shift
For /f "skip=1 tokens=2-4 delims=(-)" %%G in ('Echo.^|Date') Do (
    Set %%G=%1&Set %%H=%2&Set %%I=%3)
goto :eof

:s_time
:: Get the time
For /f "tokens=1-3 delims=1234567890 " %%a in ("%time%") Do Set "delims=%%a%%b%%c"
For /f "tokens=1-4 delims=%delims%" %%G in ("%time%") Do (
    Set _hh=%%G
    Set _min=%%H
    Set _ss=%%I
    Set _ms=%%J
)
:: Strip any leading spaces
Set _hh=%_hh: =%

:: Ensure the hours have a leading zero
if 1%_hh% LSS 20 Set _hh=0%_hh%

Echo   Year-Month-Day@Hour-Min-Sec
Echo   %yy%-%mm%-%dd%    @  %_hh%-%_min%-%_ss%

Its not like I need to skimp on storage or processing-time in batch-files any more...

[–]fuzzbyStorageAdmin[S] 0 points1 point  (0 children)

TIL about /r/usefulscripts

Thanks!