Safe way to get current day month and year in batch
batch-file, date, windows
Solution
First, you could retrieve the current settings from the registry (somewhere). The format and also the separators.
But it's easier to use `wmic` instead, it has always the same format, the only drawback is, that it's take a bit of time, each time you start it.
Modified sample from SO: Log Date/Time
@echo off
setlocal EnableDelayedExpansion
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
if "%%B" NEQ "" (
SET /A FDATE=%%F*10000+%%D*100+%%A
SET /A FTIME=%%B*10000+%%C*100+%%E
)
)
ECHO !FDATE! - !FTIME!
Problem
I am trying to write a batch script that will get the current day, month and year in a safe manner(i.e. independent of the date format). Unfortunately all the internet sources I have found propose something like: ``` SET CURRENT_DATE=%DATE: =0% SET YEAR=%DATE:~-4% SET MONTH=%DATE:~3,2% SET DAY=%DATE:~5,2% ``` This will work if my date is in format yyyy/mm/dd or yyyy-mm-dd but will not if it is in format yy-mm-dd or some other format. Is there a safe way to get the year, month and day? And one more question - is it possible to know what the day separator is(`/` in the first case and `-` in the other cases).