Batch script (cmd) resulting in DD-MM-YYYY_weekday format

29,703

Solution 1

I would like to have it formatted like DD-MM-YYYY_weekday

Below are two solutions:

  • Batch file
  • PowerShell (a simple one line solution)

The Batch file solution

First, what not to do:

  • Using %date% to provide a solution is, as used in the other answers, dependent on the OS Locale, Regional, and Language settings and may provide wrong or missing information.

  • For example, the short date format on my PC does not output the weekday at all.

The right way to solve the problem:

  • Using wmic, on the other hand, works independently of OS Locale, Language or the user's chosen date format (Control Panel/Regional).

The following batch file uses wmic to retrieve the date and (local) time, so doesn't suffer the disadvantage of a solution using %date%.

The batch file also calls PowerShell to retrieve the weekday, as that is not directly available otherwise.

getdate.cmd:

@echo off
setlocal
rem get the date
rem use findstr to strip blank lines from wmic output
for /f "usebackq skip=1 tokens=1-3" %%g in (`wmic Path Win32_LocalTime Get Day^,Month^,Year ^| findstr /r /v "^$"`) do (
  set _day=00%%g
  set _month=00%%h
  set _year=%%i
  )
rem pad day and month with leading zeros
set _month=%_month:~-2%
set _day=%_day:~-2%
rem get day of the week
for /f %%k in ('powershell ^(get-date^).DayOfWeek') do (
  set _dow=%%k
  )
rem output format required is DD-MM-YYYY_weekday
echo %_day%-%_month%-%_year%_%_dow%
endlocal

Example output:

F:\test>getdate
06-06-2016_Monday

Credits:


Alternative Powershell solution.

A simple one line PowerShell solution is:

Get-Date -format "yyyy-MM-dd_dddd"

Example output:

PS F:\test> Get-Date -format "yyyy-MM-dd_dddd"
2016-06-06_Monday

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • for /f - Loop command against the results of another command.
  • getdate - Display the date and time independent of OS Locale, Language or the users chosen date format (Control Panel/Regional).
  • variables - Extract part of a variable (substring).
  • wmic - Windows Management Instrumentation Command.

Solution 2

Try:

echo %date:~7,2%-%date:~4,2%-%date:~10,4%_%date:~0,3%

Source for manipulation tips

Solution 3

This should do it. It uses the for command to tear apart the date /t results and uses echo to construct the output. The details can be worked out at the command-line.

for /f "tokens=1,2,3,4 delims=/ " %a in ('date /t') do echo %b-%c-%d_%a

Share:
29,703

Related videos on Youtube

Anastasiya
Author by

Anastasiya

Updated on September 18, 2022

Comments

  • Anastasiya
    Anastasiya over 1 year

    I need to use date /t command and convert the date format to DD-MM-YYYY_weekday with all the "-" and "_" as listed I can't find an answer on my question. Could you please help me?

    • DavidPostill
      DavidPostill almost 8 years
      What do you mean by weekday?
    • Anastasiya
      Anastasiya almost 8 years
      for /F "tokens=1,2,3 delims=/ " %%1 in ('date /t') do set date=%%2%%3%%1 tried this one
    • Anastasiya
      Anastasiya almost 8 years
      weekday - for example, monday
    • DavidPostill
      DavidPostill almost 8 years
      @Anastasiya Done. See my answer that uses the local independent (correct) way of retrieving the required information.
  • DavidPostill
    DavidPostill almost 8 years
    Output is 01-6/-_06/.
  • uSlackr
    uSlackr almost 8 years
    @DavidPostill If you have an alternative solution, feel free to post it. Repeating the comment isn't adding much value.
  • DavidPostill
    DavidPostill almost 8 years
    @uSlackr I will be.
  • Anastasiya
    Anastasiya almost 8 years
    Now I only need to understand it :)
  • DavidPostill
    DavidPostill almost 8 years
    @Anastasiya You are welcome. I had most of the code already available, but it took a little while to figure out how the get the weekday.
  • DavidPostill
    DavidPostill almost 8 years
    It would take too long to explain ;)
  • Victor Stafusa
    Victor Stafusa about 6 years
    This have the side-effect of changing the font of my console window. It seems that it is specifically a side-effect of calling power shell.
  • DavidPostill
    DavidPostill about 6 years
    @VictorStafusa Doesn't change my font ...