Very easy to get the date and time, actually:
set Year=
for /f "skip=2" %%x in ('wmic Path Win32_LocalTime get Year^,Month^,Day^,Hour^,Minute^,Second /Format:List') do (
if not defined Year set %%x
)
I'm assuming local time here. If you need UTC, adapt it accordingly.
Your format makes things more complicated. Apologies if I get something wrong here, I'm not familiar with am/pm formats.
if %Hour% LSS 12 (
set ampm=AM
if %Hour%==0 set Hour=12
) else (
set ampm=PM
set /a Hour-=12
)
We need a few leading zeroes:
if %Month% LSS 10 set Month=0%Month%
if %Day% LSS 10 set Day=0%Day%
if %Minute% LSS 10 set Minute=0%Minute%
if %Hour% LSS 10 set Hour=0%Hour%
Then it's time to assemble the parts:
set Timestamp=%Month%-%Day%-%Year% %Hour%:%Minute% %ampm%
(Just a random note: Why on earth would you want that timestamp format?)
Best Answer
The date/time format is encoded as part of the region. You can set this from the CLI via the PowerShell
Set-Culture
cmdlet. This is part of theInternational
module available since Windows 8/Server 2012. You are basically looking for the equivalent of the GUI Control Panel Region applet (which can be displayed withcontrol /name Microsoft.RegionAndLanguage
).Example
For the impatient, the basic PowerShell command is:
There are some other date/time patterns I've listed down the bottom that you might wish to modify.
Details
First, if you're on Windows 8, you'll need to import the module. This is not necessary on Windows 10.
Now, if you wanted to change your entire region, you can do that in a single command, e.g.:
You can use any valid BCP-47 tag here. Note that this will also change other region-related settings, such as the decimal separator.
This is the approach I would recommend most of the time, unless you have specific needs otherwise.
If you want to more specifically control the date/time format, you'll need to either construct your own
CultureInfo
object or take the current culture and modify it:Relevant date/time properties
You can see the list of existing options by just running
$culture.DateTimeFormat
(orGet-Culture.DateTimeFormat
). The ones you might want to modify are:I would recommend not touching the RFC1123 pattern and the sortable patterns.