Regional Independent date time batch job

 

Got pissed off because your batch file has to know the current date and time and depending on the user or machine logged on you don’t get the same result? Let’s use today snippet: regional independent date time batch job.

In the past, you used:

  • date /t
  • time /t

But did you know there was a Win32_LocalTime which exposes date parts and time parts as properties?

Don’t know how to use that class? Look at that snippet:

<pre class="lang:batch decode:true " title="Get Date and Time irrespective of regional settings">FOR /F "skip=2 tokens=2-7 delims=," %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:csv') DO ( 
       SET Today=%%F%%D%%A 
       SET Now=%%B%%C%%E 
) 

You may change the format of date stored in today and the time in now by changing the order of the tokens’ variables.

Feel free to replace WIn32_LocalTime by Win32_UTCTime if you need to be time zone independent