Am trying to format date in cmd windows but it is not coming out nicely.
echo %date% +"%d-%B-%Y". How should I go about it?
Am trying to format date in cmd windows but it is not coming out nicely.
echo %date% +"%d-%B-%Y". How should I go about it?
You might be interested in the wmic command which can return the local date/time in a locale-independent manner.
@echo off
Title wmic command which can return the local date/time in a locale-independent manner
@for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
set today=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2%
echo Today : %today%
set "year=%MyDate:~2,2%"
set "month=%MyDate:~4,2%"
set "day=%MyDate:~6,2%"
echo %day%-%month%-%year%
pause
I belive this is what you want:
set year=%date:~10,4%
set month=%date:~4,2%
set day=%date:~7,2%
set dateformatted=%month%-%day%-%year%
echo %dateformatted%
Answer from here. Have fun!
wmic os get LocalDateTime be a better (locale independent) solution?
– Richard
Nov 19 '21 at 12:25
%date% is the worst, IMO), including wmic.
– Berend
Nov 19 '21 at 12:27
%B which will require matching a number to the full name of the month.
– Richard
Nov 19 '21 at 12:31
20211119131755.595000+000 which looks to be yyyymmddhhmmss. I have no idea what is after the . - I'm guessing sub-seconds but it's hard to tell because it takes a second for Windows to return the value.
– Richard
Nov 19 '21 at 13:20
powershell.exe get-date -Format "yyyy-MM-dd"– Tang Chanrith Sep 05 '23 at 08:51